note 85175 deleted from function.time by aidan

From: Date: Wed, 15 Apr 2009 16:09:06 +0000
Subject: note 85175 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153338@lists.php.net to get a copy of this message
Note Submitter: Jeremiah Peterson ---- I took a different approach to a time difference. The current functions provided to not account for the different number of days in a month. I have included a function that will calculate the start dates month and the number of days in each month thereafter. There are two things that I have not yet taken into account. One is daylight savings time and the other is leap year. Perhaps someone can add that to this code. function num_months($mon_start, $months, $work) { while($work > 2419200) { switch($mon_start) { case 1: case 3: case 5: case 7: case 8: case 10: if($work > 2678400) { $work = $work - 2678400; $mon_start++; $months++; } else { $exit = 1; } break; case 12: if($work > 2678400) { $work = $work - 2678400; $mon_start = 1; $months++; } else { $exit = 1; } break; case 2: $work = $work - 2419200; $mon_start++; $months++; break; case 4: case 6: case 9: case 11: if($work > 2592000) { $work = $work - 2592000; $mon_start++; $months++; } else { $exit = 1; } break; } if($exit == 1) { break; } } return Array($months,$work); } $month_start = 9; $day_start = 18; $year_start = 2007; $hour_start = 17; $min_start = 25; $sec_start = 0; $start = gmmktime($hour_start,$min_start,$sec_start, $month_start,$day_start,$year_start); $yys = (365*24*60*60); $wks = (7*24*60*60); $dys = (24*60*60); $hrs = (60*60); $mns = (60); $now = time(); $diff = $now - $start; $work = $diff; $years = (int)($work / $yys); $work = $work - $years*$yys; $total_years = pstr($years,"year",0); $months = 0; list($months, $work) = num_months($month_start, $months, $work); $total_months = pstr($months,"month",0); $weeks = (int)($work / $wks); $work = $work - $weeks*$wks; $total_weeks = pstr($weeks,"week",0); $days = (int)($work / $dys); $work = $work - $days*$dys; $total_days = pstr($days,"day",0); $hours = (int)($work / $hrs); $work = $work - $hours*$hrs; $total_hours = pstr($hours,"hour",0); $mins = (int)($work / $mns); $work = $work - $mins*$mns; $total_mins = pstr($mins,"minute",0); $secs = $work; $total_secs = pstr($secs,"second","."); print "Difference is $total_years $total_months $total_weeks $total_days $total_hours $total_mins $total_secs.<br />"; ?>

« previous php.notes (#153338) next »