huxphlux Posted July 19, 2010 Share Posted July 19, 2010 Hello everyone, had a look around on google an such, find some there, but it didnt help me at all. But hopefully someone here could help me. All help is really appriciated! So, to the "problem" or what i shall call it. I got too different dates in a database. Ive fetched them from it and stored it into a variabels. Lets say like this. $date1 = 2010-07-20 20:00:00 $date2 = 2010-07-21 20:00:00 So, these dates is just examples. So what i want to do is to find out how many hours it is between them and store the hours into a variable. And yes, they are stored like that in the database, so its not unixtime. If you need to know more, please ask. Great thanks. Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2010 Share Posted July 19, 2010 Just do directly it in your query when you retrieve the data - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff Use HOUR for the unit parameter. Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/#findComment-1088395 Share on other sites More sharing options...
Cagecrawler Posted July 19, 2010 Share Posted July 19, 2010 Use DateTime::diff (or it's procedural equivalent date_diff) $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%d days'); $datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-10-13'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%d days'); Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/#findComment-1088396 Share on other sites More sharing options...
huxphlux Posted July 19, 2010 Author Share Posted July 19, 2010 Oh thanks, still got one more question, is it possible to use like the current date in the query and compare it with one date in the database? Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/#findComment-1088398 Share on other sites More sharing options...
huxphlux Posted July 19, 2010 Author Share Posted July 19, 2010 And btw, is not that compareing and give you how many days? I would want hours.. quote author=Cagecrawler link=topic=304782.msg1441660#msg1441660 date=1279582838] Use DateTime::diff (or it's procedural equivalent date_diff) $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%d days'); $datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-10-13'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%d days'); Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/#findComment-1088399 Share on other sites More sharing options...
huxphlux Posted July 20, 2010 Author Share Posted July 20, 2010 Solved with a timestampdiff. thanks for all the help! If someone want or need the code line so post. Quote Link to comment https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/#findComment-1088406 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.