格式化日期时间

时间:2012-08-11 12:07:10

标签: php mysql html

我正在尝试格式化来自数据库的日期时间,格式为

2012-06-11 21:39:54

但我希望它以June 11

的格式显示

怎么做呢?

由于

4 个答案:

答案 0 :(得分:4)

echo date('M d', strtotime('2012-06-11 21:39:54'));

Output

答案 1 :(得分:3)

您还可以使用DateTime对象。

$date = new DateTime($yourString);
$date->format($yourFOrmat);

我认为这是最好的方式,因为DateTime比timestamp和date / strtotime函数更强大。 从上面给出的代码中,您可以添加诸如修改日期,迭代时间,比较2个日期而不使用str_to_time等函数的功能......

$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
    $date->format($yourFormat);//iterate each minute
}  

等等

答案 2 :(得分:1)

PHP手册提供了有关使用日期/时间功能的出色文档。基本上,您需要两个功能的组合:strtotime()date()

strtotime()会将您的日期转换为Unix时间戳,可以作为第二个参数提供给date()

您需要的日期格式为:M d

备用:此外,您还可以尝试不需要转换为UNIX时间戳的MYSQL对应项。记录here。假设您使用date作为日期时间字段,则需要这样的内容,

SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table

答案 3 :(得分:-1)

要使用php格式化日期,您需要传递日期的时间戳
和格式说明符作为日期函数的参数。 例如回音日期('M d',strtotime('2012-06-11 21:39:54'));