开发者

converting between date formats

how would you convert a da开发者_StackOverflow社区te stored as

2011-01-18 11:51:41

into

18-01-2011 11:51:41

using PHP?

many thanks in advance!


date('d-m-Y H:i:s', strtotime('2011-01-18 11:51:41'));


More reliable than using strtotime(), assuming you're on PHP 5.3+

$oldtime = date_parse_from_format('Y-m-d h:i:s', '2011-01-18 11:51:41');
$newtime = date('d-m-Y h:i:s', $time);

However, the date format you're converting FROM suggests it's coming from a MySQL datetime field, in which case you could also do:

SELECT DATE_FORMAT(yourfield, '%d-%m-%Y %H:%i:%s')

and save yourself a full roundtrip in PHP.


Convert the old date to UNIX time with strtotime(), then output it in the new format with date()

$olddate = "2011-01-18 11:51:41";
$newdate = date('d-m-Y H:i:s', strtotime($olddate));
echo $newdate;

// 18-01-2011 11:51:41


$your_date = "2011-01-18 11:51:41";
echo date('d-m-Y H:i:s', strtotime($your_date));

demo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜