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.
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
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