eranwein Posted February 28, 2006 Share Posted February 28, 2006 i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end? Quote Link to comment https://forums.phpfreaks.com/topic/3721-arithmetics-with-dates/ Share on other sites More sharing options...
khburres Posted March 2, 2006 Share Posted March 2, 2006 [!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [snapback]350130[/snapback][/div][div class=\'quotemain\'][!--quotec--]i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?[/quote]Convert each to their seconds equivalent, subtract the earlier from the latter, then convert number of days, minutes, etc.How are the dates formatted? Are these MySQL date/times? Quote Link to comment https://forums.phpfreaks.com/topic/3721-arithmetics-with-dates/#findComment-13583 Share on other sites More sharing options...
grim1208 Posted March 2, 2006 Share Posted March 2, 2006 [!--quoteo(post=350130:date=Feb 27 2006, 11:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 11:28 PM) [snapback]350130[/snapback][/div][div class=\'quotemain\'][!--quotec--]i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?[/quote]You would want to start off with seperating the date into an array with the explode function and seperate each into month, day, year, hour, minutes, seconds etc.Also be careful with the dates you will want to look into the gregorian and julian format, this will help set off the different amount of days in the month. Let me know if you need me to expand in my explanationedit: google "php explode" or look in the php manual, also google "php GregorianToJD' or look in the php manual. I won't write out the script for you, doing just a little search will definately benifit you. Reply this with what you have come up with or questions and I would be more than happy to assist :) Quote Link to comment https://forums.phpfreaks.com/topic/3721-arithmetics-with-dates/#findComment-13584 Share on other sites More sharing options...
obsidian Posted March 2, 2006 Share Posted March 2, 2006 [!--quoteo(post=350130:date=Feb 27 2006, 10:28 PM:name=eranwein)--][div class=\'quotetop\']QUOTE(eranwein @ Feb 27 2006, 10:28 PM) [snapback]350130[/snapback][/div][div class=\'quotemain\'][!--quotec--]i have two dates which are formed by date and time. how do i get the difference between them. let's say i need the number of days or hours that pased between start and end?[/quote]when you say "formed by date and time," are you referring to a SQL DATETIME (YYYY-MM-DD HH:MM:SS)?if so, then simply use the strtotime() function on it, and do some arithmetic on the difference between them:[code]function getDayDiff($date1, $date2) { $diff = abs(strtotime($date1) - strtotime($date2)); $day = 60 * 60 * 24; // seconds in a day return floor($diff / $day);}function getHourDiff($date1, $date2) { $diff = abs(strtotime($date1) - strtotime($date2)); $hour = 60 * 60; // seconds in an hour return floor($diff / $hour);}$date1 = date('Y-m-d H:i:s'); // right now$date2 = '2005-12-25 00:00:00'; // midnight last christmas morningecho getDayDiff($date1, $date2) . " Days since last Christmas!<br />\n";echo getHourDiff($date1, $date2) . " Hours since last Christmas!<br />\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3721-arithmetics-with-dates/#findComment-13590 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.