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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19152347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:55:53  来源:igfitidea点击:

Formatting date to human readable format

phpmysqlunix-timestampdate-formatting

提问by soniccool

Lets say in my mysql database I have a timestamp 2013-09-30 01:16:06and lets say this variable is $ts.

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

How can I output this to show more like September 30th, 2013?

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

回答by Juan Cortés

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

Note the use of Sto get the english ordinal suffix for the day.
Since you're already using strtotimeif you need a human readable data for the current timeyou can just use the keyword "now" as in strtotime("now")

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

Related sources

相关资源

回答by Hanky Panky

Use strtotime()to convert that string into a Unix Timestamp, then use the date()function to display it like you want.

使用strtotime()该字符串转换为Unix时间戳,然后使用date()功能就像要显示它。

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

Reference:

参考:

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

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

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

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

回答by BlitZ

I do not think that php is even needed there. Check MySQL DATE_FORMAT()function.

我认为那里甚至不需要 php。检查 MySQLDATE_FORMAT()功能。

Example:

例子:

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

Result:

结果:

September 30th, 2013 01:16:06

For real usage:

实际使用:

SELECT
    DATE_FORMAT(`date_column`, '%M %D, %Y %H:%i:%s') as `readable`
FROM
    `your_table`;

回答by Burak ?ztürk

echo date("F d Y",strtotime("2013-09-30 01:16:06"))

回答by Anup Singh

Mysql query

mysql查询

 select UNIX_TIMESTAMP(datetime_field) as datetime_field from table;

PHP

PHP

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

回答by Shudmeyer

Something like:

就像是:

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

回答by Dinesh Naik

Use the diffForHumansfunction:

使用diffForHumans函数:

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