note 42085 added to function.mktime

From: Date: Mon, 03 May 2004 08:23:53 +0000
Subject: note 42085 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-69126@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42085 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42085&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42085&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69126) next »