note 85175 added to function.time
| From: | JeremiahPeterson at osu1 dot php dot net | Date: | Tue, 19 Aug 2008 01:55:18 +0000 |
| Subject: | note 85175 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143619@lists.php.net to get a copy of this message | ||
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 />";
?>
----
Server IP: 209.41.74.194
Probable Submitter: 68.104.22.184
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/85175
Del: integrated -- https://master.php.net/note/delete/85175/integrated
Del: useless -- https://master.php.net/note/delete/85175/useless
Del: bad code -- https://master.php.net/note/delete/85175/bad+code
Del: spam -- https://master.php.net/note/delete/85175/spam
Del: non-english -- https://master.php.net/note/delete/85175/non-english
Del: in docs -- https://master.php.net/note/delete/85175/in+docs
Del: other reasons-- https://master.php.net/note/delete/85175
Reject -- https://master.php.net/note/reject/85175
Search -- https://master.php.net/manage/user-notes.php