hassank1 Posted January 2, 2009 Share Posted January 2, 2009 Hi I've retreived a mysql (date/time) field from a database .. and I want to compare it with the current time .. (for example return 1 if > , 0 if = , -1 if < ) Note : I also want the time 2 be compared not only if the date are the same .. ex same date but different times then they are not equal.. thx Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/ Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 Do it in MySQL itself http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/#findComment-727955 Share on other sites More sharing options...
hassank1 Posted January 2, 2009 Author Share Posted January 2, 2009 I will check that..thx however I still need a php function or method to do the same task. Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/#findComment-727974 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 Easiest would probably be to convert date retrieved from database to unix datestamp using strtotime and compare it with current time as returned by time Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/#findComment-727975 Share on other sites More sharing options...
hassank1 Posted January 2, 2009 Author Share Posted January 2, 2009 aha gr8 .. thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/#findComment-727976 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 Here's something I've come up with. Not tested //$timestamp1,$timestamp2 should be unix timestamps function compareTime($timestamp1,$timestamp2) { if ($timestamp1 == $timestamp2) { return 0; } else { return ($timestamp1 - $timestamp2)/ abs($timestamp1 - $timestamp2); } } echo compareTime(strtotime($dateFromMySQL),time()); Quote Link to comment https://forums.phpfreaks.com/topic/139181-solved-comparing-dates/#findComment-727977 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.