note 115734 added to datetime.format

From: Date: Mon, 15 Sep 2014 12:02:07 +0000
Subject: note 115734 added to datetime.format
Groups: php.notes 
Request: Send a blank email to php-notes+get-200447@lists.php.net to get a copy of this message
The udate function is a great start, but the formatting of the milliseconds is a little off. If it is within the first 100000 microseconds then the string will be less than 6 characters, so 0.012435 will appear as 0.12345. The revision below fixes this. function udate($strFormat = 'u', $uTimeStamp = null) { // If the time wasn't provided then fill it in if (is_null($uTimeStamp)) { $uTimeStamp = microtime(true); } // Round the time down to the second $dtTimeStamp = floor($uTimeStamp); // Determine the millisecond value $intMilliseconds = round(($uTimeStamp - $dtTimeStamp) * 1000000); // Format the milliseconds as a 6 character string $strMilliseconds = str_pad($intMilliseconds, 6, '0', STR_PAD_LEFT); // Replace the milliseconds in the date format string // Then use the date function to process the rest of the string return date(preg_replace('(?<!\\\\)u', $strMilliseconds, $strFormat), $dtTimeStamp); } ---- Server IP: 72.52.91.14 Probable Submitter: 96.30.146.154 ---- Manual Page -- http://php.net/manual/en/datetime.format.php Edit -- https://master.php.net/note/edit/115734 Del: integrated -- https://master.php.net/note/delete/115734/integrated Del: useless -- https://master.php.net/note/delete/115734/useless Del: bad code -- https://master.php.net/note/delete/115734/bad+code Del: spam -- https://master.php.net/note/delete/115734/spam Del: non-english -- https://master.php.net/note/delete/115734/non-english Del: in docs -- https://master.php.net/note/delete/115734/in+docs Del: other reasons-- https://master.php.net/note/delete/115734 Reject -- https://master.php.net/note/reject/115734 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200447) next »