Jump to content

PHP Date comparasion


_tina_

Recommended Posts

Hi,

 

I have a MySql date in a variable.  I need to test if it is three days after that date.  Do you know how to do this?

 

Maybe something like:

if ( $date == date('Y-m-d', strtotime("+3 days")) ) ..

 

but I can't seem to get it just right.

 

Thanks in advance!

Link to comment
Share on other sites

You could ask MySQL to do that for you, I think (I forget how, off the top of my head, but MySQL has some nifty date functions) ..

 

Or, you'd have to do a straight-up text match on it, which would mean you'd have to make sure the date in $date looked exactly like you needed it to look.

 

I forget what MySQL dates look like now, (I usually store dates as unix timestamps, since it's easier to manipulate those - checking for +3 days is as simple as checking for $date+(3 days worth of seconds, whatever that is))

 

But what you're doing is producing a date from PHP that will look like this: "2009-09-14" (for example) .. PHP will then try a pure text match against whatever the date from MySQL is. If the date from MySQL looks like this: "09/14/2009: 12:34", it won't match, even though technically it's the same day.

 

Link to comment
Share on other sites

Thanks for the reply.  Yes, my date will look like - 2009-09-21 00:00:00 -

 

You could ask MySQL to do that for you, I think (I forget how, off the top of my head, but MySQL has some nifty date functions) ..

 

Or, you'd have to do a straight-up text match on it, which would mean you'd have to make sure the date in $date looked exactly like you needed it to look.

 

I forget what MySQL dates look like now, (I usually store dates as unix timestamps, since it's easier to manipulate those - checking for +3 days is as simple as checking for $date+(3 days worth of seconds, whatever that is))

 

But what you're doing is producing a date from PHP that will look like this: "2009-09-14" (for example) .. PHP will then try a pure text match against whatever the date from MySQL is. If the date from MySQL looks like this: "09/14/2009: 12:34", it won't match, even though technically it's the same day.

Link to comment
Share on other sites

If that's what the mysql date looks like (i.e. the value of $date in your code), then you need to make sure the php one will come out at the same format, so you'll need something like:

 

date("Y-m-d 00:00:00");

 

Because string-wise:

 

"2009-09-21 00:00:0" does not equal "2009-09-21" (which is what your original date format would have produced.

 

Uh.. Make sense?

Link to comment
Share on other sites

If you wanna check if today is three days after the date stored in $date, you have to subtract three days instead of adding them:

 

if ($date == date('Y-m-d 00:00:00', strtotime('-3 days')))

Will only work if the time in $date is always 00:00:00. If not, you can use

 

list($date) = explode(' ', $date);
if ($date == date('Y-m-d', strtotime('-3 days')))

Link to comment
Share on other sites

Great, this is just what I need.

 

Thanks for the help with this. :)

 

If you wanna check if today is three days after the date stored in $date, you have to subtract three days instead of adding them:

 

if ($date == date('Y-m-d 00:00:00', strtotime('-3 days')))

Will only work if the time in $date is always 00:00:00. If not, you can use

 

list($date) = explode(' ', $date);
if ($date == date('Y-m-d', strtotime('-3 days')))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.