note 42085 deleted from function.mktime by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 07 Nov 2004 20:28:37 +0000 |
| Subject: | note 42085 deleted from function.mktime by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80245@lists.php.net to get a copy of this message | ||
Note Submitter: bichinhoverde at spwinternet dot com dot br
----
Here is a simple function replacement to mktime and gmmktime, for *any* range of hour, min, sec,
day, month or year, including negatives and very big ones:
function my_mktime($hour, $min, $sec, $month, $day, $year) {
return my_gmmktime($hour, $min, $sec, $month, $day, $year)
+
mktime(0,0,0,1,1,2000)
-
gmmktime(0,0,0,1,1,2000);
}
function my_gmmktime($hour, $min, $sec, $month, $day, $year) {
$days_month = array(31,31,28,31,30,31,30,31,31,30,31,30);
$output = 0;
if ( $month <= 0 ) {
$year -= floor(-$month/12)+1;
$month = $month%12 + 12;
}
for ( $i = 1970 ; $i < $year ; $i++ ) {
$output += 365*24*60*60;
if ( $i % 4 == 0 ) {
$output += 24*60*60;
}
}
for ( $i = $year ; $i < 1970 ; $i++ ) {
$output -= 365*24*60*60;
if ( $i % 4 == 0 ) {
$output -= 24*60*60;
}
}
for ( $i = 1 ; $i < $month ; $i++ ) {
$output += $days_month[$i%12]*24*60*60;
if ( ( $i % 12 == 2 ) && ( ($year+floor($i/12)) % 4 == 0 ) ) {
$output += 24*60*60;
}
}
$output += ($day-1)*24*60*60;
$output += $hour*60*60;
$output += $min*60;
$output += $sec;
return $output;
}
Note that these functions will not consider DST.