Reputation: 9714
I've getting a DATETIME from the database and I need to be able to check to see whether or not that date is within 30 days from now.
$renewal_date = $row['renewal_date'];
$current_time = new DateTime();
$interval = $renewal_date->diff($current_time);
That ain't workin' for me.
Upvotes: 1
Views: 99
Reputation: 6122
Try something like this
$renewal_date = new DateTime($row['renewal_date']);
$cutoff= new DateTime('+31 days 00:00');
if ($renewal_date < $cutoff && $renewal_date >= new DateTime)
echo 'Renewal Time!';
else
echo 'All OK!';
Remove the && condition if you want to show renewal for dates in the past
Upvotes: 2
Reputation: 10348
$renewal_date = strtotime($renewal_date); // maybe need it depending of sql format of date
if (abs($renewal_date - time()) < 86400*30)
{
echo 'less than 30 days from now';
}
EDIT 1
abs($renewal_date - time())
gives a lapsus of time from now (time()
) that it is supposed to refer to future. indeed the condition could to serve to past periods too...
< 86400*30
says that the lapsus must be inferior to 30 days...
That were my 2 cents to the cause :)
Upvotes: 0
Reputation: 31141
$renewal_date
is not a PHP DateTime object. You can do var_dump($renewal_date);
to verify this.
Check out the DateTime::diff manual for examples. You'll want to convert your date to a DateTime object using something like this:
$datetime1 = new DateTime($renewal_date);
Then it should work.
Upvotes: 0
Reputation: 2820
Are you sure that $row['renewal_date'] is a DateTime object?
My impression is that this is a value that you take from a database rather than a DateTime object.
Try a var_dump($renewal_date) and if this is a string containing a date value, you should use something like:
$renewal_date = new DateTime ($row['renewal_date']);
$current_time = new DateTime();
$interval = $renewal_date->diff($current_time);
Upvotes: 0