I am trying to determine whether a date is in the future or not, using DateTime
objects but it always comes back positive:
$opening_date = new DateTime($current_store['openingdate']);
$current_date = new DateTime();
$diff = $opening_date->diff($current_date);
echo $diff->format('%R'); // +
if($diff->format('%R') == '+' && $current_store['openingdate'] != '0000-00-00' && $current_store['openingdate'] !== NULL) {
echo '<img id="openingsoon" src="/img/misc/openingsoon.jpg" alt="OPENING SOON" />';
}
The problem is it's always positive so the image shows when it shouldn't be.
I must be doing something stupid, but what is it, it's driving me insane!
we can analyze the dates by simple comparison operator if the given dates are in a similar format. <? php $date1 = "2018-11-24"; $date2 = "2019-03-26"; if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>
The difference between P and p format is that the new p format uses Z for UTC time, while the P format uses +00:00 . The ISO 8601 date format permits UTC time with +00:00 format, or with Z . This means both date formats below are valid: 2020-09-09T20:42:34+00:00.
It's easier than you think. You can do comparisons with DateTime objects:
$opening_date = new DateTime($current_store['openingdate']); $current_date = new DateTime(); if ($opening_date > $current_date) { // not open yet! }
You don't need a DateTime
object for this. Try this:
$now = time();
if(strtotime($current_store['openingdate']) > $now) {
// then it is in the future
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With