_tina_ Posted September 22, 2009 Share Posted September 22, 2009 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! Quote Link to comment Share on other sites More sharing options...
Raphael diSanto Posted September 22, 2009 Share Posted September 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
_tina_ Posted September 22, 2009 Author Share Posted September 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
Raphael diSanto Posted September 22, 2009 Share Posted September 22, 2009 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? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 22, 2009 Share Posted September 22, 2009 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'))) Quote Link to comment Share on other sites More sharing options...
_tina_ Posted September 22, 2009 Author Share Posted September 22, 2009 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'))) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.