在PHP中将日期格式从FyD的jS转换为Ymd


Convert Date Format from jS of F Y D to Ymd in PHP

我有以下格式的日期

31st of August 2014 Sun使用$cursor->format("jS 'of F Y D");,但我如何将现有时间转换为Ymd,使输出为20140831

我试过像echo date("Y-m-d", strtotime(31st of August 2014 Sun));

我不知道为什么输出会像1970年1月1日那样,这很奇怪。

我也试过

echo contime('1st of August 2013 Sun');
function contime($gettime) {
$fetch_date = strtotime($gettime);
$date = new DateTime('');
return $date->format('Ymd');
}

但这是今天的日期。

还尝试在PHP 中将一种日期格式转换为另一种

$old_date = date('1st of August 2013 Sun');  //date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);   
echo $new_date;

但其打印1970-01-01 00:00:00

<?php
$date = DateTime::createFromFormat("jS 'of F Y D", '31st of August 2014 Sun');
echo $date->format('Ymd');

但请注意,字符串必须是正确的,在您的示例中您有:

'1st of August 2013 Sun'

如果2013年8月1日不是星期日,则是星期日,如果数据集中的日期名称不正确,则应将其删除。

您应该将时间划分为日-月-年-分钟和秒(您需要将月转换为数字),然后您应该能够使用mktime:

 echo date("Y-m-d", mktime($hour, $minute, $second, $month, $day, $year));