PHP Date Comparison Not Working -
i'm trying understand why code works:
<?php // $today = date("d/m/y"); $today = date("y-m-d"); $today_dt = new datetime($today); foreach ($records $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 today time':'expire time greater today time';?></td> but when change format of date y-m-d d/m/y fails error:
<?php // $today = date("d/m/y"); $today = date("d/m/y"); $today_dt = new datetime($today); foreach ($records $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 today time' : 'expire time greater today time';?></td> the error
'php fatal error: uncaught exception 'exception' message 'datetime::__construct(): failed parse time string (19/06/2015) @ position 0 (1)'
the line references is:
$today_dt = new datetime($today);
the date format used should 'mm/dd/yyyy', not 'dd/mm/yyyy'.
Comments
Post a Comment