我试图将日期从yyyy-mm-dd转换为dd-mm-yyyy(但不是SQL);然而,我不知道日期函数如何需要时间戳,我无法从这个字符串中获得时间戳。

这怎么可能呢?


当前回答

使用strtotime()和date():

$original_dateTime = "2019-05-11 17:02:07"; #This may be database datetime
$newDate = date("d-m-Y", strtotime($original_dateTime));

随着时间的推移

$newDate = date("d-m-Y h:i:s a", strtotime($original_dateTime));

其他回答

在PHP中,任何日期都可以转换为所需的日期格式,使用不同的场景,例如将任何日期格式转换为 日、日、月、年

$newdate = date("D, d M Y", strtotime($date));

它将以以下非常好的格式显示日期

2020年11月16日星期一

Use:

implode('-', array_reverse(explode('-', $date)));

如果没有日期转换开销,我不确定它会有多大影响。

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

有关更多信息,请参阅strtotime的文档。

或者更短:

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

有两种实现方法:

1.

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

2.

    $cls_date = new DateTime($date);
    echo $cls_date->format('d-m-Y');
date('m/d/Y h:i:s a',strtotime($val['EventDateTime']));