将日期格式化为人类可读的格式


Formatting date to human readable format

假设在我的 mysql 数据库中,我有一个时间戳2013-09-30 01:16:06,假设这个变量是$ts .

我怎样才能输出它以显示更像September 30th, 2013

$timestamp = "2013-09-30 01:16:06";
echo date("F jS, Y", strtotime($timestamp)); //September 30th, 2013

请注意使用 S 来获取当天的英语序号后缀。
由于您已经在使用strtotime如果您需要当前时间的人类可读数据,因此您只需使用关键字"now"即可strtotime("now")

相关来源

  • 日期
  • 斯特托时间
使用

strtotime() 将该字符串转换为 Unix 时间戳,然后使用 date() 函数根据需要显示它。

echo date("F j, Y, g:i a",strtotime($ts)); 

参考:

http://www.php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/function.date.php

我认为那里甚至不需要php。检查 MySQL DATE_FORMAT()函数。

例:

SELECT DATE_FORMAT('2013-09-30 01:16:06', '%M %D, %Y %H:%i:%s') as `readable`;

结果:

September 30th, 2013 01:16:06

对于实际使用:

SELECT
    DATE_FORMAT(`date_column`, '%M %D, %Y %H:%i:%s') as `readable`
FROM
    `your_table`;
echo date("F d Y",strtotime("2013-09-30 01:16:06"))

像这样:

<?php
$date = '2013-09-30 01:16:06';
$convertDate = date('F jS, Y h:i:s', strtotime($date));
echo $convertDate;
?>

Mysql 查询

 select UNIX_TIMESTAMP(datetime_field) as datetime_field from table;

.PHP

echo date("F d Y", $datetime_field )

使用 diffForHumans 函数:

Carbon'Carbon::parse($ts)->diffForHumans()