开发者

converting between date formats

开发者 https://www.devze.com 2023-03-22 21:58 出处:网络
how would you convert a da开发者_StackOverflow社区te stored as 2011-01-18 11:51:41 into 18-01-2011 11:51:41

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

精彩评论

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

关注公众号