Here is what I have so far:
public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) {
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
list($year_start,$month_start,$day_start) = explode('-',date('Y-m-d',$time_start));
list($year_end,$month_end,$day_end) = explode('-',date('Y-m-d',$time_end));
$years = $year_end - $year_start;
$months = $month_end - $month_start;
$days = $day_start - $day_end;
$weeks = 0;
$hours = 0;
$mins = 0;
$secs = 0;
if(mktime(0,0,0,$month_end,$day_end) < mktime(0,0,0,$month_start,$day_start)) {
$years -= 1;
}
if($days < 0) {
$months -= 1;
$days += 30;
}
if($months < 0) $months += 12;
if(strpos($format, '%y')===false) {
$months += $years * 12;
}
if(strpos($format, '%w')!==false) {
$weeks = floor($days/7);
$days %= 7;
}
echo date('Y-m-d',$time_start).' to '.date('Y-m-d',$time_end).": {$years}y {$months}m {$weeks}w {$days}d<br/>";
}
(This is incomplete and inaccurate)
I can't seem to get the math right. Its naive division will not work due to leap years and different lengths of months.
The logic should also vary depending on the format string. For example, passing from 04 to February 2010 to June 28, 2011 (as unix timestamps) with a format string %y year %m month %d dayshould output 1 year 4 month 24 day, but if %y yearomitted, it is necessary to add 12 months to a month, i.e., there should be a way out 16 month 24 day.
Should also handle the time ... but I haven't figured it out yet.
date_diff . , date_diff, .
, $diff->format , ... , " " . :
>>> $start = new DateTime('04-Feb-2010')
>>> $end = new DateTime('28-Jun-2011')
>>> $diff = $start->diff($end)
>>> $diff->format('%m months, %d days')
'4 months, 24 days'
16 months, 24 days, . , , , . , , , , , , , .
,
" " , , .