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

这怎么可能呢?


当前回答

还有一种模糊的可能性:

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

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

其他回答

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

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

或者更短:

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

下面给出的PHP代码使用mktime()生成明天的日期,并将其格式更改为dd/mm/yyyy格式,然后使用echo打印它。

$tomorrow = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
echo date("d", $tomorrow) . "/" . date("m", $tomorrow). "/" . date("Y", $tomorrow);

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

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);
}

使用date_create和date_format

试试这个。

function formatDate($input, $output){
  $inputdate = date_create($input);
  $output = date_format($inputdate, $output);
  return $output;
}

您可以使用date()和strtotime()更改格式。

$date = '9/18/2019';

echo(’d-m-y’日期、strtotime(美元);

结果:

The 18-09-19

我们可以通过改变(d-m-y)来改变格式。