Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mySQL当前时间戳_Php_Mysql - Fatal编程技术网

Php mySQL当前时间戳

Php mySQL当前时间戳,php,mysql,Php,Mysql,嘿,我一直在做一个关于php和mysql的测验 我使用了当前的时间戳来跟踪用户回答特定问题的时间。现在我想打印出时间,我在数据库中做了一行,名为“timestamp” 它可以很好地跟踪哪些用户首先应答,但当我回显该行时,它看起来是这样的: 2012-01-11 17:14:50 所以我的问题是: 我该如何在时间和日期上分解这些内容 我的数据库中不能有2行来存储时间,因为正如我所说的,我使用当前的时间戳来跟踪哪些用户首先回答 现在打印出来的方式看起来不太好看,而且使用起来也不太灵活 希望任何人都

嘿,我一直在做一个关于php和mysql的测验

我使用了当前的时间戳来跟踪用户回答特定问题的时间。现在我想打印出时间,我在数据库中做了一行,名为“timestamp”

它可以很好地跟踪哪些用户首先应答,但当我回显该行时,它看起来是这样的:

2012-01-11 17:14:50
所以我的问题是:

我该如何在时间和日期上分解这些内容

我的数据库中不能有2行来存储时间,因为正如我所说的,我使用当前的时间戳来跟踪哪些用户首先回答

现在打印出来的方式看起来不太好看,而且使用起来也不太灵活

希望任何人都能帮忙

非常感谢你的阅读


Mathias。

您可以使用MySQL以任何方式格式化它,也可以使用PHP函数将其转换为Unix时间戳,然后在其上使用该函数

mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
        -> 'Sunday October 2009'
或:


听起来你只是想把日期时间格式化成一种美观的方式。查看此网站:查看各种格式,找到您最喜欢的格式,并将其添加到您的select语句中。

通读一遍


我总是喜欢从MYSQL获取unix时间戳,因此在sql查询中:

SELECT UNIX_TIMESTAMP(time) as time FROM table
其中
time
是php中的列名:

$in_format_time = date("H:i d M Y T",$time);
echo $in_format_time;
其中,
$time
是获取的结果。这将输出以下内容:1970年1月1日19:00 GMT

但您可以在date函数中使用您喜欢的格式,请查看以下内容:

DATE\u格式(现在(),“%W,%M%e,%Y@%h:%i%p”)

2008年9月20日星期日下午12:45

MySQL DATE_FORMAT() Letter Representations
Specifier   Description
%a          Abbreviated weekday name (Sun..Sat)
%b          Abbreviated month name (Jan..Dec)
%c          Month, numeric (0..12)
%D          Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d          Day of the month, numeric (00..31)
%e          Day of the month, numeric (0..31)
%f          Microseconds (000000..999999)
%H          Hour (00..23)
%h      Hour (01..12)
%I      Hour (01..12)
%i      Minutes, numeric (00..59)
%j      Day of year (001..366)
%k      Hour (0..23)
%l      Hour (1..12)
%M      Month name (January..December)
%m      Month, numeric (00..12)
%p      AM or PM
%r      Time, 12-hour (hh:mm:ss followed by AM or PM)
%S      Seconds (00..59)
%s      Seconds (00..59)
%T      Time, 24-hour (hh:mm:ss)
%U      Week (00..53), where Sunday is the first day of the week
%u      Week (00..53), where Monday is the first day of the week
%V      Week (01..53), where Sunday is the first day of the week; used with %X
%v      Week (01..53), where Monday is the first day of the week; used with %x
%W      Weekday name (Sunday..Saturday)
%w      Day of the week (0=Sunday..6=Saturday)
%X      Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x      Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y      Year, numeric, four digits
%y      Year, numeric (two digits)
%%      A literal “%” character
%x          x, for any “x” not listed above

我会在该站点上使用MySQL文档,我也会这样做,但很明显,作者是新的,MySQL文档(或任何RDBMS文档)可能会让人望而生畏。该网站将帮助他们了解不同格式掩码的作用以及如何使用它们,并为他们提供示例sql,向他们展示如何在select语句中使用它。这就是我向作者推荐它的原因。这个网站真的很有帮助!我使用它没有问题,非常感谢!尝试
echo date('m/d/y',strottime($yourvable))抱歉,我使用的是美国日期格式,不知道您在哪里,但您可以通过查看此处来创建自己的掩码。如果您使用时间来确定谁先回答,您可能希望在时间中添加ms。为什么要将日期和时间分开?它们对于确定实际发生的时间都是必不可少的,我从来没有看到将它们分开存储的好理由。如果您只是想单独显示它们,请改为这样做。数据库用于数据存储。一般来说,应用程序应该处理格式和不应该处理的内容。
$in_format_time = date("H:i d M Y T",$time);
echo $in_format_time;
MySQL DATE_FORMAT() Letter Representations
Specifier   Description
%a          Abbreviated weekday name (Sun..Sat)
%b          Abbreviated month name (Jan..Dec)
%c          Month, numeric (0..12)
%D          Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d          Day of the month, numeric (00..31)
%e          Day of the month, numeric (0..31)
%f          Microseconds (000000..999999)
%H          Hour (00..23)
%h      Hour (01..12)
%I      Hour (01..12)
%i      Minutes, numeric (00..59)
%j      Day of year (001..366)
%k      Hour (0..23)
%l      Hour (1..12)
%M      Month name (January..December)
%m      Month, numeric (00..12)
%p      AM or PM
%r      Time, 12-hour (hh:mm:ss followed by AM or PM)
%S      Seconds (00..59)
%s      Seconds (00..59)
%T      Time, 24-hour (hh:mm:ss)
%U      Week (00..53), where Sunday is the first day of the week
%u      Week (00..53), where Monday is the first day of the week
%V      Week (01..53), where Sunday is the first day of the week; used with %X
%v      Week (01..53), where Monday is the first day of the week; used with %x
%W      Weekday name (Sunday..Saturday)
%w      Day of the week (0=Sunday..6=Saturday)
%X      Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x      Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y      Year, numeric, four digits
%y      Year, numeric (two digits)
%%      A literal “%” character
%x          x, for any “x” not listed above