Convert date format yyyy-mm-dd => dd-mm-yyyy

2018-12-31 02:51发布

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.

How is this possible?

12条回答
伤终究还是伤i
2楼-- · 2018-12-31 02:58
$newDate = preg_replace("/(\d+)\D+(\d+)\D+(\d+)/","$3-$2-$1",$originalDate);

This code works for every date format.

You can change the order of replacement variables such $3-$1-$2 due to your old date format.

查看更多
唯独是你
3楼-- · 2018-12-31 03:00
date('m/d/Y h:i:s a',strtotime($val['EventDateTime']));
查看更多
冷夜・残月
4楼-- · 2018-12-31 03:04
$timestamp = strtotime(your date variable); 
$new_date = date('d-m-Y', $timestamp);

For more:

click here

Or even shorter:

$new_date = date('d-m-Y', strtotime(your date variable));
查看更多
柔情千种
5楼-- · 2018-12-31 03:05

Also another obscure possibility:

$oldDate = '2010-03-20'
$arr = explode('-', $oldDate);
$newDate = $arr[2].'-'.$arr[1].'-'.$arr[0];

I don't know if I would use it but still :)

查看更多
无色无味的生活
6楼-- · 2018-12-31 03:06

two ways to implement this

1)

$date = strtotime(date); 
$new_date = date('d-m-Y', $date);

2)

$cls_date = new DateTime($date);
echo $cls_date->format('d-m-Y'); 
查看更多
心情的温度
7楼-- · 2018-12-31 03:07

You can try strftime function. Something like strftime($time, '%d %m %Y');

查看更多
登录 后发表回答