-2

From an API call i get the following date I suppose as a string format : [end_date] => 2018-09-20

Now I have my own created date in my PHP script: $date = '2017-09-20';

Now I want to check if end_date from the API request is larger than my own created date string.

I tried the following :

  if($req['end_date'] > strtotime('2019-12-01') || !$req['end_date'])

It didnt work tho. Help is appreciated!

Also tried :

  if(strtotime($req['end_date']) > strtotime('2019-12-01') || !$req['end_date'])

The if statement should also pass when there is not end_date from the api response. Thats what the or is for.

Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • 1
    *It didnt work* is not an error description. Describe what you get and what you expect. BTW debugging the code can help – Jens Jan 06 '20 at 16:11
  • You might do better with a `|| ! isset($req['end_date'])` – RiggsFolly Jan 06 '20 at 16:13
  • There is no error it simply didnt pass the test , as i'm getting dates back that shouldn't have passed the test implemented. @Jens – Kevin.a Jan 06 '20 at 16:14

1 Answers1

1

Use the DateTime class. https://www.php.net/manual/en/class.datetime

$theirDate = new DateTime($theirDateString);
$yourDate = new DateTime($yourDateString);

if ($theirDate > $yourDate) {
    // voila!
}

I'm sure you know how to check for an actual date string before creating the objects too ;-)

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39