1

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.

4
  • Is $renewal_date a DateTime object? Commented Sep 8, 2012 at 5:10
  • strtotime is a good place to start I think php.net/manual/en/function.strtotime.php Commented Sep 8, 2012 at 5:11
  • That doesn't make it a PHP DateTime object. Do var_dump($renewal_date); to see. Commented Sep 8, 2012 at 5:12
  • check whether $renewal_date and $current_time are in same format...???(like 'mm-dd-yy' or 'yy-dd-mm') Commented Sep 8, 2012 at 5:18

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm starting to notice a flaw ... the chances of a CRON job being able to lang exactly on "30 days" from now it slim to none. So how would I be able to catch that renewal within reason?
I am not sure what you mean? I upated my answer so that it will always be midnight of 30th day. So if you run your cron every day it will always check fine. remember it is < not ==
Yeah, what you did above is exactly what I needed!
+1 Ah! now I see it. DateTimeclass really exists! but be careful, is 5.2+ compatible...
0

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);

Comments

0

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

Comments

0
$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 :)

7 Comments

Seconds in a day. A bad way of doing this comparison IMO.
@sachleen why it is? 30days=86400secs*30 exactly.
Because it's not obvious what's going on from reading it. Also leap second.
Yeah, but @sachleen you haven't exactly given an answer to how to find out if the "renewal_date" is 30 days of less then NOW
@sachleen I know what are you saying I am working with more fine exactitude, but question clearly shows that it is a simple comparison. 30 days...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.