Jump to content

PHP Date comparasion


_tina_

Recommended Posts

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
https://forums.phpfreaks.com/topic/175133-php-date-comparasion/#findComment-923008
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
https://forums.phpfreaks.com/topic/175133-php-date-comparasion/#findComment-923016
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
https://forums.phpfreaks.com/topic/175133-php-date-comparasion/#findComment-923030
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
https://forums.phpfreaks.com/topic/175133-php-date-comparasion/#findComment-923040
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
https://forums.phpfreaks.com/topic/175133-php-date-comparasion/#findComment-923059
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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