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

这怎么可能呢?


当前回答

使用此函数可以从任何格式转换为任何格式

function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
    $date_aux = date_create_from_format($from_format, $date);
    return date_format($date_aux,$to_format);
}

其他回答

还有一种模糊的可能性:

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

我不知道我是否会使用它,但仍然:)

对于这种特定的转换,我们还可以使用格式字符串。

$new = vsprintf('%3$s-%2$s-%1$s', explode('-', $old));

显然,这不适用于许多其他日期格式转换,但由于我们在本例中只是重新排列子字符串,因此这是另一种可能的方法。

使用此函数可以从任何格式转换为任何格式

function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
    $date_aux = date_create_from_format($from_format, $date);
    return date_format($date_aux,$to_format);
}

您可以尝试strftime()函数。简单示例:strftime($time, '%d %m %Y');

date('m/d/Y h:i:s a',strtotime($val['EventDateTime']));