note 57844 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:39:26 +0000 |
| Subject: | note 57844 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101819@lists.php.net to get a copy of this message | ||
Note Submitter: jbricci at ya-right dot net
----
date function for windows supporting dates before 1-1-1970
usage....
negtime2date ( date_format, time_stamp, off_set );
if no off_set is passed or off_set = 0, gmdate for the timestamp is returned. Other wise the date
formatted is based on the off_set
see my function under * gmmktime * for a example on how to create negative timestamps (dates before
1-1-1970)
<?
// usage...
echo negtime2date ( 'D, F d, Y H:i:s A', -1514196929, -5 ); // Jan 7, 1922 - 13:44:31 PM
GMT
function negtime2date ( $tf, $ts, $to = 0 )
{
$ta = array ( 60, 3600, 86400, 365 );
$ts += ( $to * $ta[1] );
if ( $ts < 0 )
{
$i = 11;
$ad = 0;
$wd = 3;
$od = $ta[3];
$tt = $to[0];
$to = substr( $to, 1 );
$mm = '00';
if ( strpos ( $to, '.' ) )
{
$am = array ( 5 => 30, 25 => 15, 75 => 45 );
$tp = explode ( '.', $to );
$to = $tp[0];
$mm = isset ( $am[$tp[1]] ) ? $am[$tp[1]] : '00';
}
$ap = array ( 'am', 'AM', 'pm', 'PM' );
$sd = array ( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat' );
$ld = array ( 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday' );
$sm = array ( 'Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec' );
$lm = array ( 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December' );
$oa = array ( 0, 0, 0, 12, 0, 1969 );
$ma = array ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
while ( $ts < ( -( $ta[3] + $ad ) * $ta[2] ) )
{
$ad = 0;
$td = 1;
$ma[1] = 28;
if ( ( $oa[5] % 4 ) == 0 && ( $oa[5] % 100 > 0 || $oa[5] % 400 == 0 ) )
{
$ad = 1;
$td = 2;
$ma[1] = 29;
}
while ( $td-- )
{
$wd = $wd ? $wd : 7;
$wd--;
$wd = ! $wd && $td ? 7 : $wd;
}
$ts += ( $ta[3] + $ad ) * $ta[2];
$oa[5] -= 1;
}
$od += $ad;
while ( $ts < ( -$ma[$i] * $ta[2] ) )
{
$ts += ( $ma[$i] * $ta[2] );
$oa[3] -= 1;
$od -= $ma[$i];
$td = ( $ma[$i] - 28 );
while ( $td-- )
{
$wd = $wd ? $wd : 7;
$wd--;
$wd = ! $wd && $td ? 7 : $wd;
}
$i--;
}
$oa[4] = ( $ma[$i] + ceil ( ( $ts + 1 ) / $ta[2] ) );
$td = ( $ma[$i] - $oa[4] );
while ( $td-- )
{
$wd = $wd ? $wd : 7;
$wd--;
$wd = ! $wd && $td ? 7 : $wd;
$od -= 1;
}
$ts += ( ( $ma[$i] - $oa[4] + 1 ) * $ta[2] );
$oa[0] = floor ( $ts / $ta[1] );
$ts -= ( $oa[0] * $ta[1] );
$oa[1] = floor ( $ts / $ta[0] );
$oa[2] = ( $ts - $oa[1] * $ta[0] );
$new = array (
'z' => ( $od ? ( $od - 1 ) : 0 ),
'W' => ( $od <= 7 ? 1 : round ( $od / 7 ) ),
's' => sprintf ( '%02s', $oa[2] ),
'i' => sprintf ( '%02s', $oa[1] ),
'g' => ( $oa[0] > 12 ? ( $oa[0] - 12 ) : $oa[0] ),
'G' => $oa[0],
'h' => sprintf ( '%02s', ( $oa[0] > 12 ? ( $oa[0] - 12 ) : $oa[0] ) ),
'H' => sprintf ( '%02s', $oa[0] ),
't' => $ma[$i],
'L' => ( 29 - $ma[1] ),
'w' => $wd,
'N' => ( 1 + $wd ),
'm' => sprintf ( '%02s', $oa[3] ),
'n' => $oa[3],
'd' => sprintf ( '%02s', $oa[4] ),
'y' => substr ( $oa[5], 2 ),
'Y' => $oa[5],
'j' => $oa[4],
'S' => ( ( $s = substr ( $oa[4], - 1 ) ) == 1 ? 'st' : ( $s == 2 ?
'nd' : ( $s == 3 ? 'rd' : 'th' ) ) ),
'F' => $lm[$i],
'M' => $sm[$i],
'D' => $sd[$wd],
'l' => $ld[$wd],
'a' => ( $oa[0] >= 12 ? $ap[2] : $ap[0] ),
'A' => ( $oa[0] >= 12 ? $ap[3] : $ap[1] ),
'O' => sprintf ( $tt . '%02s' . $mm, $to ),
'Z' => ( ( $tt == '-' ? $tt : null ) . ( ( $to * $ta[1] ) + ( $mm * $ta[0]
) ) ),
'c' => sprintf ( $oa[5] . '-%02s-%02sT%02s:%02s:%02s' . $tt .
'%02s:' . $mm, $oa[3], $oa[4], $oa[0], $oa[1], $oa[2], $to ),
'r' => sprintf ( $sd[$wd] . ', %02s ' . $sm[$i] . ' ' . $oa[5] .
' %02s:%02s:%02s ' . $tt . '%02s' . $mm, $oa[4], $oa[0], $oa[1], $oa[2], $to )
);
return ( strtr ( $tf, $new ) );
}
else
{
return ( gmdate ( $tf, $ts ) );
}
}
?>