note 49954 modified in function.gmmktime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:19:23 +0000 |
| Subject: | note 49954 modified in function.gmmktime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142050@lists.php.net to get a copy of this message | ||
I have seen many different hacked versions of this function for people using Windows that want to
support dates before Jan 1, 1970. Here is yet another one that is different than others you may have
seen! It does not use loops like all the others I have seen!
<?php
// usage...
echo win_gmmktime ( 12, 43, 16, 07, 23, 1946 );
function win_gmmktime ( $hour, $minute, $second, $month, $day, $year )
{
if ( $year > 1969 )
{
return ( gmmktime ( $hour, $minute, $second, $month, $day, $year ) );
}
$t = 0;
$ds = 86400;
$hs = 3600;
$dy = 365;
$ms = 60;
$months = array ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
$leap_year = $year % 4 == 0 && ( $year % 100 > 0 || $year % 400 == 0 ) ? true : false;
if ( $year < 1969 )
{
$y = 1969 - $year;
$t -= ( $y * $dy ) * $ds;
$x = ceil ( $y / 4 );
if ( $leap_year && $month > 2 )
{
$x -= 1;
}
$t -= $x * $ds;
}
if ( $month != 12 )
{
$tm = $months;
$tm = array_slice ( $tm, $month );
$t -= array_sum ( $tm ) * $ds;
unset ( $tm );
}
$nh = ( ( $month == 2 && $leap_year ? 29 : $months[$month-1] ) - $day );
$t -= $nh != 0 ? $nh * $ds : 0;
$nh = 23 - $hour;
$t -= $nh != 0 ? $nh * $hs : 0;
$nh = 59 - $minute;
$t -= $nh != 0 ? $nh * $ms : 0;
$nh = 59 - $second;
$t -= $nh != 0 ? $nh + 1 : 0;
return ( $t );
}
?>
--was--
I have seen many different hacked versions of this function for people using Windows that want to
support dates before Jan 1, 1970. Here is yet another one that is different than others you may have
seen! It does not use loops like all the others I have seen!
<?
// usage...
echo win_gmmktime ( 12, 43, 16, 07, 23, 1946 );
function win_gmmktime ( $hour, $minute, $second, $month, $day, $year )
{
if ( $year > 1969 )
{
return ( gmmktime ( $hour, $minute, $second, $month, $day, $year ) );
}
$t = 0;
$ds = 86400;
$hs = 3600;
$dy = 365;
$ms = 60;
$months = array ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
$leap_year = $year % 4 == 0 && ( $year % 100 > 0 || $year % 400 == 0 ) ? true : false;
if ( $year < 1969 )
{
$y = 1969 - $year;
$t -= ( $y * $dy ) * $ds;
$x = ceil ( $y / 4 );
if ( $leap_year && $month > 2 )
{
$x -= 1;
}
$t -= $x * $ds;
}
if ( $month != 12 )
{
$tm = $months;
$tm = array_slice ( $tm, $month );
$t -= array_sum ( $tm ) * $ds;
unset ( $tm );
}
$nh = ( ( $month == 2 && $leap_year ? 29 : $months[$month-1] ) - $day );
$t -= $nh != 0 ? $nh * $ds : 0;
$nh = 23 - $hour;
$t -= $nh != 0 ? $nh * $hs : 0;
$nh = 59 - $minute;
$t -= $nh != 0 ? $nh * $ms : 0;
$nh = 59 - $second;
$t -= $nh != 0 ? $nh + 1 : 0;
return ( $t );
}
?>
http://php.net/manual/en/function.gmmktime.php