我正试图使用以下代码在表中显示日期
现在,假设要显示的日期是2013年12月12日,那么echo$date1将日期显示为2013-12-12。但echo date("j-F-Y", strtotime($date1));
显示日期为1969年12月31日。
<script id="template" type="text/x-handlebars-template">
<table>
<tr>
<th>Posted on: </th>
<td><?php $date1='{{posted}}';echo $date1 ;echo date("j-F-Y", strtotime($date1));?></td>
</tr>
</table>
</script>
我应该如何显示以$date1打印的日期。为什么date()无法显示正确的日期。
strtotime在失败时返回false或-1(5.1之前)。时间戳(0)将等于1970-01-01 00:00:00 GMT。时区位于"减去某个值"中,等于1969-12-31年的日期。
因此strtotime($date1)
无法解析$date1
。您确定它等于字符串'2013-12-12'
吗?使用var_dump($date1);
进行验证。
它可能是一个具有__toString()
-方法的对象,因此echo将显示该方法的结果,而不是$date1
本身的实际值。
strtotime方法在2013-12-12期间返回的内容超出了预期。请参见此处:http://www.php.net/manual/de/function.strtotime.php#100144
对此进行补偿的最佳方法是修改您的加入字符。正斜杠(/)表示美式M/D/Y格式破折号(-)表示欧洲D-M-Y,句点(.)表示ISOY.M.D.
所以您必须在strtotime使用另一种格式。这也解释了为什么date在1970年1月1日前一天返回值。