我試圖理解爲什麼這個代碼工作:PHP日期比較不工作
<?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 Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (19/06/2015) at position 0 (1)'
它引用該生產線是:
$today_dt = new DateTime($today);
謝謝 - 這是有效的。我不明白爲什麼我必須這樣做。你可以解釋嗎? – user982124
您可以在此頁面獲取解釋[date_create](http://php.net/manual/en/function.date-create.php) – perilla