日期格式仅显示当年

Date format showing only current year

$ddd=`1 December, 2014`;
$from = date("Y-m-d 00:00:00", strtotime($ddd));
echo $from.

Returns 2015-12-01 00:00:00 而不是 2014-12-01 00:00:00

有解决办法吗?

<?php
$ddd='1 December 2014';
 $from = date("Y-m-d 00:00:00", strtotime($ddd));
 echo $from;
?>

demo 您应该从日期“2014 年 12 月 1 日”中删除“,”

参考这个答案。有效。

 $ddd='1 December 2014';
    $from = date("Y-m-d 00:00:00", strtotime($ddd));
    echo $from;

strtottime() 文档中,它说:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

因此,您应该将 $ddd 转换为 acceptable date format before using strtotime(). Even better, if you have PHP 5.3+, you can use the DateTime::createFromFormat() 函数:

$date = DateTime::createFromFormat('!d F, Y', '1 December, 2014');

其中 $date 是一个 DateTime 对象。

strtotime只理解一个specific set of formats。如果您的输入不是其中一种格式,它会尽力猜测,但如您所见,结果可能会有所不同。

试试这个:

 <?php
    $ddd = '1 December, 2014';
    $t = date_create_from_format("d M, Y",$ddd);
    $from = date_format($t,"Y-m-d 00:00:00");
    echo $from;

 ?>