格式化日期更漂亮 php


Format date more nicely php

所以目前我有 3 个变量将日期作为整数,将月份作为月份的 3 个字母表示,将年份保存为 4 位数字。

所以例如

$day = '16';
$month = 'nov';
$year = '2013';

现在,如果我想像这样显示它

November 16th 2013

我在想我必须从我的数据中生成一个 unix 时间戳,然后使用

date('F jS Y', $timestamp);

不过,我不确定如何正确生成 unix 时间戳。

这将执行您想要的操作:

echo date('F jS Y', strtotime("$month $day $year"));

关键部分是 strtotime() 函数,它将为您将日期字符串解析为时间戳。

工作示例:http://3v4l.org/INV8d

按照OOP的方式,很酷;)

<?php 
$day = '16';
$month = 'nov';
$year = '2013';
$newDate="$day-$month-$year";
$date_new = DateTime::createFromFormat('d-M-Y',$newDate); 
echo $newformat=$date_new->format('F dS Y'); //November 16th 2013
?>