citricsquidut78696 Posted July 21, 2008 Share Posted July 21, 2008 Hi, I've found a couple of solutions on this, however everything I've written only works for certain date formats and it also messes up when the date goes over a year. I have the date in the format Day/Month/year - So 28/09/1991 would be the 28th of September 1991. Now, I need to work out the amount of days between 28/09/1991 and 18/02/1996 for example. How would I do this? The data would be coming in from a database, so it would be coming in that date format, d/m/y. Any help much appreciated Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/ Share on other sites More sharing options...
marcus Posted July 21, 2008 Share Posted July 21, 2008 <?php $date1 = mktime(0,0,0,9,28,1991); $date2 = mktime(0,0,0,2,18,1996); $diff = $date2 - $date1; $day = 86400; echo floor($diff/$day) . " days"; ?> Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/#findComment-595851 Share on other sites More sharing options...
craygo Posted July 21, 2008 Share Posted July 21, 2008 try this function days($date1, $date2){ $month1 = date("m", strtotime($date1)); $day1 = date("d", strtotime($date1)); $year1 = date("Y", strtotime($date1)); $month2 = date("m", strtotime($date2)); $day2 = date("d", strtotime($date2)); $year2 = date("Y", strtotime($date2)); $first = gregoriantojd($month1, $day1, $year1); $second = gregoriantojd($month2, $day2, $year2); $diff = abs($first-$second); return $diff; } Now you can use your dates from the database $days = days($date1, $date2); Ray Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/#findComment-595852 Share on other sites More sharing options...
citricsquidut78696 Posted July 21, 2008 Author Share Posted July 21, 2008 See, this is what's getting at me, everything doesn't work. I'm doing; function days($date1, $date2){ $month1 = date("m", strtotime($date1)); $day1 = date("d", strtotime($date1)); $year1 = date("Y", strtotime($date1)); $month2 = date("m", strtotime($date2)); $day2 = date("d", strtotime($date2)); $year2 = date("Y", strtotime($date2)); $first = gregoriantojd($month1, $day1, $year1); $second = gregoriantojd($month2, $day2, $year2); $diff = abs($first-$second); return $diff; } Then to call it; $date1 = "28/09/1992"; $date2 = "28/09/1993"; $days = days($date1, $date2); And it doesn't do anything ;| This is starting to get beyond annoying, I've spent hours trying to solve this and EVERY solution doesn't work. Where am I going wrong? Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/#findComment-595865 Share on other sites More sharing options...
kenrbnsn Posted July 21, 2008 Share Posted July 21, 2008 This function assumes that a date with slashes in it is in the form "mm/dd/yyyy" not "dd/mm/yyyy". It would be best to pass the dates in the form "yyyy-mm-dd". Ken Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/#findComment-595869 Share on other sites More sharing options...
citricsquidut78696 Posted July 21, 2008 Author Share Posted July 21, 2008 This function assumes that a date with slashes in it is in the form "mm/dd/yyyy" not "dd/mm/yyyy". It would be best to pass the dates in the form "yyyy-mm-dd". Ken Brilliant, thanks. It's not perfect, but I can work with that, just involves messing with user input, but I can manage that Thanks posters <3. Link to comment https://forums.phpfreaks.com/topic/115888-solved-the-number-of-days-between-2-dates/#findComment-595875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.