Jump to content

Date Difference not working


MGLP

Recommended Posts

I get "Object of class DateInterval could not be converted to int" for my code (2nd line)

[$mytoday=date('Y'."-".'m'."-".'d');

if (date_diff(date_create($mytoday),date_create($Row[23]))<32)] 

Note: My $mytoday = 2018-04-27  and my $Row[23] = 2018-04-26

Yet, the PHP tutorial says:

[$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);]

So my code should work but it doesn't.

I put the exact code given by the PHP tutorial in my PHP file and it doesn't work.  I get the same error message as with my code.

Thanks for your help.

 

Link to comment
Share on other sites

No, it shouldn't work, because date_diff returns a DateInterval object.  You are doing this:

if ($DateIntervalObject < 32)

You're comparing a DateInterval object with a number, hence the error that the DateInterval object could not be converted to an integer.  This is the DateInterval object:

http://php.net/manual/en/class.dateinterval.php

It has a days property that you can use to get the number of days, if the object was created using date_diff.

  • Like 1
Link to comment
Share on other sites

Did you not understand what this returned?

echo $diff->format("%R%a days"); 

ie positive or negative symbol, number of days value then the word 'days'. You need to strip this down to just the value of days. test with echo, the once an integer value is return you know it will be correct to use in if condition.

http://php.net/manual/en/dateinterval.format.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...