Difference in dates

Can’t seem to find the difference in dates here…the IF statement isn’t returning true. I’m importing dates via simpleXML and xpath…

$today = new DateTime();
$today->format('Y-m-d');
foreach($xml->xpath('//event') as $item)
{ 
  $deadline=new DateTime($item->deadline);//2015-06-11 = $item->deadline
  if($today->diff($deadline)>14)
    print "<option value=".$item->name.">".$item->name."</option>"; 
}

If I echo out $item->deadline you can see (in my comment) what it outputs. That’s correct. The $today is formatted correctly. Why is it not calculating correctly?

The foreach is finding the correct elements. I can echo out $item->deadline in my foreach loop and no issues. The dates are there.

Here’s the XML file just for sh**s and giggles.

<?xml version="1.0" encoding="UTF-8"?>
<movementorders>
    <event>
        <name>Africa</name>
        <deadline>2015-05-19</deadline>
    </event>
    <event>
        <name>Europe</name>
        <deadline>2015-06-11</deadline>
    </event>
</movementorders>

Edit- There is this warning but I do not know where to go from here

[07-Apr-2015 07:46:30 America/Denver] PHP Notice: Object of class DateInterval could not be converted to int in /home1/codefund/public_html/test/index.php on line 27

DateTime->diff() returns an instance of DateInterval, not a number. (just like the error tells you)

you would have to format that again to get the number of days.

How would I go about this? What function?

DateInterval::format()

Thank you. Why does this not work? I trid my best to format the results and even went off examples from that link.

    $today = new DateTime();
    $today->format('Y-m-d');
    foreach($xml->xpath('//event') as $item)
    { 
      $deadline=new DateTime($item->deadline);//2015-06-11 = $item->deadline
      $interval=$deadline->diff($today);
      $interval->format('%a');
      if($interval>14)
        print "<option value=".$item->name.">".$item->name."</option>"; 
    }

if you don’t save the number of days in a variable …

format() does not change the object itself, it returns a value derived from the object.

PS. I’m not aware that an object can change itself to a scalar value.

1 Like

Alright it seems to work now!

    $today = new DateTime();
    $today->format('Y-m-d');
    foreach($xml->xpath('//event') as $item)
    { 
      $deadline=new DateTime($item->deadline);//2015-06-11 = $item->deadline
      $interval=$deadline->diff($today);
      $interval=$interval->format('%a');
      if($interval>14)
        print "<option value=".$item->name.">".$item->name."</option>"; 
    }

Any flaws?

No direct flaws, the only thing you might want to consider is adding the timezone as well when creating the DateTime object. Right now the code assume that both the server and the time in the XML document is in the same timezone.

That is one of the beautiful things when working with DateTime, that it is very simple to switch between different timezones, first create the object in the correct timezone, then after switch to another one.

You can also make the code slightly more effective by changing:

$interval=$interval->format('%a'); if($interval>14)
with

if ($interval->format('%a') > 14)

Thanks for the help! I’ll update accordingly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.