note 26331 added to function.mktime

From: Date: Sat, 26 Oct 2002 20:48:47 +0000
Subject: note 26331 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-38596@lists.php.net to get a copy of this message
for the problem with -1 with processing on year before 1970 in linux & Win, I write a simple function to calculate the mktime. But the function must input the correct date/time for calculation and not support auto date correction (eg -1 / 45 in day field). Hope php can native support mktime then no need to call glibc and user using WIN PHP have no problem with it. function is_leaf_year($year) { $year = year_digit_check($year); if ($year % 400 == 0) { return true; } elseif ($year % 100 == 0 ) { return false; } elseif ($year % 4 == 0 ) { return true; } else { return false; } } function year_digit_check ($year) { if ($year < 100) { if ($year < 70) { $year = $year + 2000; } else { $year = $year + 1900; } } return $year; } function get_gmt_different() { $result = mktime(0,0,0,1970,1,1); $result2 = gmmktime(0,0,0,1970,1,1); return $result - $result2; } function my_mktime($hr,$min,$sec,$mon,$day,$year) { $gmt_different = get_gmt_different(); $hr = intval($hr); $min = intval($min); $sec = intval($sec); $mon = intval($mon); $day = intval($day); $year = intval($year); $year = year_digit_check($year); $_day_power = 86400; $_hour_power = 3600; $_min_power = 60; $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31); $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31); $_total_date = 0; if ($year >= 1970) { for ($a = 1970 ; $a <= $year; $a++) { $leaf = is_leaf_year($a); if ($leaf == true) { $loop_table = $_month_table_leaf; $_add_date = 366; } else { $loop_table = $_month_table_normal; $_add_date = 365; } if ($a < $year) { $_total_date += $_add_date; } else { for($b=1;$b<$mon;$b++) { $_total_date += $loop_table[$b]; } } } $_total_date +=$day-1; return $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different; } else { for ($a = 1969 ; $a >= $year; $a--) { $leaf = is_leaf_year($a); if ($leaf == true) { $loop_table = $_month_table_leaf; $_add_date = 366; } else { $loop_table = $_month_table_normal; $_add_date = 365; } if ($a > $year) { $_total_date += $_add_date; } else { for($b=12;$b>$mon;$b--) { $_total_date += $loop_table[$b]; } } } $_total_date += $loop_table[$mon] - $day; $_day_time = $hr * $_hour_power + $min * $_min_power + $sec; $_day_time = $_day_power - $_day_time; return -( $_total_date * $_day_power + $_day_time - $gmt_different); } } -- http://www.php.net/manual/en/function.mktime.php http://master.php.net/manage/user-notes.php?action=edit+26331 http://master.php.net/manage/user-notes.php?action=delete+26331 http://master.php.net/manage/user-notes.php?action=reject+26331

« previous php.notes (#38596) next »