-2

I've converted a string ('2019-11-13T09:00Z') to an array as below using:

$new_date = date_parse($close_date[0]);

This is the resulting array:

Array ( [year] => 2019 [month] => 11 [day] => 13 [hour] => 9 [minute] => 0 [second] => 0 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => 1 [zone_type] => 2 [zone] => 0 [is_dst] => [tz_abbr] => Z )

How do I compare it to today's date, ie. if the date (I know it's not really a date, but just an array) in the format above is in the future or past/today? Obviously, the 'if' statement is not a problem, it's comparing the date formatted as above is where I'm stuck.

Thanks

edit: as per the comments below, I've modified the wording of the question slightly.

Wasteland
  • 4,889
  • 14
  • 45
  • 91

1 Answers1

1

The simplest way is to convert the string not to an array, but to a DateTime object. You can compare the objects directly with each other.

$date = new DateTime($close_date[0]);
$now = new DateTime();
if($date > $now) {

}

However if you want to neglect the time part of the DateTime, you must normalize the dates. Set the time to midnight in both objects.

$date = (new DateTime($close_date[0]))->setTime(0, 0);
$now = (new DateTime())->setTime(0, 0);
if ($date > $now) {
    // Only true if the date part is greater than today
}

Important point! Your date strings are already in ISO 8601 format, which means you can compare the strings with each other without creating the DateTime objects. You must be sure though, that the strings will always be in the right format.

Dharman
  • 30,962
  • 25
  • 85
  • 135