I have two dates of the form:
Start Date: 2007-03-24 End Date: 2009-06-26 Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days How can I do this in PHP?
I have two dates of the form:
Start Date: 2007-03-24 End Date: 2009-06-26 Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days How can I do this in PHP?
In for a penny, in for a pound: I have just reviewed several solutions, all providing a complex solution using floor() that then rounds up to a 26 years 12 month and 2 days solution, for what should have been 25 years, 11 months and 20 days!!!!
here is my version of this problem: may not be elegant, may not be well coded, but provides a more closer proximity to a answer if you do not count LEAP years, obviously leap years could be coded into this, but in this case - as someone else said, perhaps you could provide this answer:: I have included all TEST conditions and print_r so that you can see more clearly the construct of the results:: here goes,
// set your input dates/ variables::
$ISOstartDate = "1987-06-22"; $ISOtodaysDate = "2013-06-22"; // We need to EXPLODE the ISO yyyy-mm-dd format into yyyy mm dd, as follows::
$yDate[ ] = explode('-', $ISOstartDate); print_r ($yDate);
$zDate[ ] = explode('-', $ISOtodaysDate); print_r ($zDate);
// Lets Sort of the Years! // Lets Sort out the difference in YEARS between startDate and todaysDate :: $years = $zDate[0][0] - $yDate[0][0]; // We need to collaborate if the month = month = 0, is before or after the Years Anniversary ie 11 months 22 days or 0 months 10 days... if ($months == 0 and $zDate[0][1] > $ydate[0][1]) { $years = $years -1; } // TEST result echo "\nCurrent years => ".$years; // Lets Sort out the difference in MONTHS between startDate and todaysDate :: $months = $zDate[0][1] - $yDate[0][1]; // TEST result echo "\nCurrent months => ".$months; // Now how many DAYS has there been - this assumes that there is NO LEAP years, so the calculation is APPROXIMATE not 100% // Lets cross reference the startDates Month = how many days are there in each month IF m-m = 0 which is a years anniversary // We will use a switch to check the number of days between each month so we can calculate days before and after the years anniversary switch ($yDate[0][1]){ case 01: $monthDays = '31'; break; // Jan case 02: $monthDays = '28'; break; // Feb case 03: $monthDays = '31'; break; // Mar case 04: $monthDays = '30'; break; // Apr case 05: $monthDays = '31'; break; // May case 06: $monthDays = '30'; break; // Jun case 07: $monthDays = '31'; break; // Jul case 08: $monthDays = '31'; break; // Aug case 09: $monthDays = '30'; break; // Sept case 10: $monthDays = '31'; break; // Oct case 11: $monthDays = '30'; break; // Nov case 12: $monthDays = '31'; break; // Dec }; // TEST return echo "\nDays in start month ".$yDate[0][1]." => ".$monthDays; // Lets correct the problem with 0 Months - is it 11 months + days, or 0 months +days??? $days = $zDate[0][2] - $yDate[0][2] +$monthDays; echo "\nCurrent days => ".$days."\n"; // Lets now Correct the months to being either 11 or 0 Months, depending upon being + or - the years Anniversary date // At the same time build in error correction for Anniversary dates not being 1yr 0m 31d... see if ($days == $monthDays ) if($days < $monthDays && $months == 0) { $months = 11; // If Before the years anniversary date } else { $months = 0; // If After the years anniversary date $years = $years+1; // Add +1 to year $days = $days-$monthDays; // Need to correct days to how many days after anniversary date }; // Day correction for Anniversary dates if ($days == $monthDays ) // if todays date = the Anniversary DATE! set days to ZERO { $days = 0; // days set toZERO so 1 years 0 months 0 days }; echo "\nTherefore, the number of years/ months/ days/ \nbetween start and todays date::\n\n"; printf("%d years, %d months, %d days\n", $years, $months, $days); the end result is:: 26 years, 0 months, 0 days
That's how long I have been in business for on the 22nd June 2013 - Ouch!
$date = '2012.11.13'; $dateOfReturn = '2017.10.31'; $substract = str_replace('.', '-', $date); $substract2 = str_replace('.', '-', $dateOfReturn); $date1 = $substract; $date2 = $substract2; $ts1 = strtotime($date1); $ts2 = strtotime($date2); $year1 = date('Y', $ts1); $year2 = date('Y', $ts2); $month1 = date('m', $ts1); $month2 = date('m', $ts2); echo $diff = (($year2 - $year1) * 12) + ($month2 - $month1); function showTime($time){ $start = strtotime($time); $end = strtotime(date("Y-m-d H:i:s")); $minutes = ($end - $start)/60; // years if(($minutes / (60*24*365)) > 1){ $years = floor($minutes/(60*24*365)); return "From $years year( s ) ago"; } // monthes if(($minutes / (60*24*30)) > 1){ $monthes = floor($minutes/(60*24*30)); return "From $monthes monthe( s ) ago"; } // days if(($minutes / (60*24)) > 1){ $days = floor($minutes/(60*24)); return "From $days day( s ) ago"; } // hours if(($minutes / 60) > 1){ $hours = floor($minutes/60); return "From $hours hour( s ) ago"; } // minutes if($minutes > 1){ $minutes = floor($minutes); return "From $minutes minute( s ) ago"; } } echo showTime('2022-05-05 21:33:00');