我试图理解为什么这段代码有效:
<?php
// $today = date("d/m/Y");
$today = date("Y-m-d");
$today_dt = new DateTime($today);
foreach ($records as $record) {
}
?>
<tr>
<?php $dueDate = date("Y-m-d", strtotime($record->getField('Due Date'))); ?>
<td><?php echo 'today is: ' . $today; ?></td>
<td><?php echo 'expiry date is: ' . $dueDate; ?></td>
<td> <?php $expire_dt = new DateTime($dueDate); ?> </td>
<td><?php echo $expire_dt < $today_dt ? 'expire time less than today time':'expire time greater than today time';?></td>
但是当我将日期格式从Y-m-d
更改为d/m/Y
时,会失败并显示错误:
<?php
// $today = date("d/m/Y");
$today = date("d/m/Y");
$today_dt = new DateTime($today);
foreach ($records as $record) {
}
?>
<tr>
<?php $dueDate = date("d/m/Y", strtotime($record->getField('Due Date'))); ?>
<td><?php echo 'today is: ' . $today; ?></td>
<td><?php echo 'expiry date is: ' . $dueDate; ?></td>
<td><?php $expire_dt = new DateTime($dueDate); ?></td>
<td><?php echo $expire_dt < $today_dt ? 'expire time less than today time' : 'expire time greater than today time';?></td>
错误是
'PHP致命错误:带有消息的未捕获异常'异常' 'DateTime :: __ construct():无法解析时间字符串(19/06/2015)at 位置0(1)'
它引用的行是:
$today_dt = new DateTime($today);
答案 0 :(得分:0)
使用DateTime类,您不必按照希望的方式格式化输入日期,您可以使用格式以任何格式显示它。
// Get todays date
$today_dt = new DateTime();
// Display date in specified format
echo $today_dt->format('d/m/Y');
答案 1 :(得分:0)
您使用的日期格式应为'mm / dd / yyyy',而不是'dd / mm / yyyy'。