note 112519 added to function.gmdate

From: Date: Wed, 26 Jun 2013 00:07:54 +0000
Subject: note 112519 added to function.gmdate
Groups: php.notes 
Request: Send a blank email to php-notes+get-195351@lists.php.net to get a copy of this message
If you want to handle years past 2038 and/or show microseconds, use this substitute: function fgmdate($f, $t = NULL) { // like gmdate, but accepts fractional timestamps // and has no problem with years past 2038 if (is_null($t)) $t = microtime(true); $days = (int)($t/86400); $secs = $t - $days*86400; if ($secs<0) { $days--; $secs+=86400; } $usec = $secs; $secs = (int)$secs; $usec-=$secs; $usec = (int)(($usec*1000000)+0.5); if ($usec>999999) { $usec=0; $secs++; } if ($secs>86399) { $secs=0; $days++; } $hh = (int)($secs/3600); $mi = (int)(($secs%3600)/60); $ss = $secs%60; $pm = ($hh>=12); $hx = $hh%12; if ($hx==0) $hx=12; $beat = ($secs+3600)*10+(int)($usec/100000); $beat = (int)($beat/864)%1000; $abc = array(); $abc['e'] = 'UTC'; $abc['I'] = '0'; $abc['O'] = '+0000'; $abc['P'] = '+00:00'; $abc['T'] = 'GMT'; $abc['Z'] = '0'; $abc['a'] = $pm ? 'pm' : 'am'; $abc['A'] = $pm ? 'PM' : 'AM'; $abc['B'] = ($beat<100?'0':'').($beat<10?'0':'').$beat; $abc['g'] = ''.$hx; $abc['G'] = ''.$hh; $abc['h'] = ($hx<10?'0':'').$hx; $abc['H'] = ($hh<10?'0':'').$hh; $abc['i'] = ($mi<10?'0':'').$mi; $abc['s'] = ($ss<10?'0':'').$ss; $abc['u'] = ($usec<100000?'0':'').($usec<10000?'0':''). ($usec<1000?'0':'').($usec<100?'0':'').($usec<10?'0':'').$usec; $rdays = $days + 280872; $wd = $rdays % 7; $iwd = $wd; if ($iwd==0) $iwd = 7; $yyyy = 1201; $ddd = $rdays; $yyyy += 400*(int)($ddd/146097); $ddd %= 146097; if ($ddd) { while ($ddd>36524) { $yyyy+=100; $ddd-=36524; } $yyyy += 4*(int)($ddd/1461); $ddd %= 1461; if ($ddd) { while ($ddd>365) { $yyyy++; $ddd-=365; } } else { $yyyy--; $ddd=366; } } else { $yyyy--; $ddd=366; } $leap = (($yyyy%100==0)?($yyyy%400==0):($yyyy%4==0)); $ycal = array(1 => 31,28,31,30,31,30, 31,31,30,31,30,31); if ($leap) $ycal[2]++; $mm = 1; $dd = $ddd; while ($dd>$ycal[$mm]) { $dd-=$ycal[$mm]; $mm++; } $mlen = $ycal[$mm]; $wkyr = $yyyy; $wch = $ddd - $iwd + 10; $ww = (int)($wch/7); if ($wch > ($leap?372:371)) { $wkyr++; $ww = 1; } else if ($wch < 7) { $wkyr--; $wch += ((($wkyr%100==0)?($wkyr%400==0):($wkyr%4==0))?366:365); $ww = (int)($wch/7); } $yy = $yyyy % 100; $dzero = $ddd - 1; $mlist = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $wlist = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); $slist = array("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"); $suff = ((($dd<10)||($dd>20)) ? $slist[$dd%10] : 'th'); $abc['Y'] = ''.$yyyy; $abc['y'] = ($yy<10?'0':'').$yy; $abc['L'] = ($leap?'1':'0'); $abc['n'] = ''.$mm; $abc['m'] = ($mm<10?'0':'').$mm; $abc['F'] = $mlist[$mm]; $abc['M'] = substr($mlist[$mm],0,3); $abc['t'] = ''.$mlen; $abc['o'] = ''.$wkyr; $abc['W'] = ($ww<10?'0':'').$ww; $abc['z'] = ''.$dzero; $abc['j'] = ''.$dd; $abc['d'] = ($dd<10?'0':'').$dd; $abc['S'] = $suff; $abc['w'] = ''.$wd; $abc['l'] = $wlist[$wd]; $abc['D'] = substr($wlist[$wd],0,3); $abc['N'] = ''.$iwd; $abc['c'] = $abc['Y'].'-'.$abc['m'].'-'.$abc['d'].'T'. $abc['H'].':'.$abc['i'].':'.$abc['s'].$abc['P']; $abc['r'] = $abc['D'].', '.$abc['d'].' '.$abc['M'].' '.$abc['Y'].' '. $abc['H'].':'.$abc['i'].':'.$abc['s'].' '.$abc['O']; $abc['U'] = ''.($days*86400+$secs); $fa = str_split($f); $fac = count($fa); $pos = 0; while ($pos < $fac) { if ($fa[$pos]=='\\') { $fa[$pos] = ''; $pos+=2; } else if (array_key_exists($fa[$pos],$abc)) { $fa[$pos] = $abc[$fa[$pos]]; $pos++; } else $pos++; } $out = implode($fa); return $out; } ---- Server IP: 192.41.42.26 Probable Submitter: 207.210.135.237 ---- Manual Page -- http://php.net/manual/en/function.gmdate.php Edit -- https://master.php.net/note/edit/112519 Del: integrated -- https://master.php.net/note/delete/112519/integrated Del: useless -- https://master.php.net/note/delete/112519/useless Del: bad code -- https://master.php.net/note/delete/112519/bad+code Del: spam -- https://master.php.net/note/delete/112519/spam Del: non-english -- https://master.php.net/note/delete/112519/non-english Del: in docs -- https://master.php.net/note/delete/112519/in+docs Del: other reasons-- https://master.php.net/note/delete/112519 Reject -- https://master.php.net/note/reject/112519 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195351) next »