note 97441 deleted from function.date by derick

From: Date: Sun, 15 Aug 2010 18:36:41 +0000
Subject: note 97441 deleted from function.date by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171702@lists.php.net to get a copy of this message
Note Submitter: fabiocbalbuquerque at gmail dot com ---- I made this function so I can convert microtime (microseconds) into millitime (milliseconds). In this particular case, I have a function that when I give a valid Date, following or not by a pattern (like 'Y-m-d') and using the micro_to_millis intermediate function, it returns the given Date in milliseconds. By the other side, the date_from_millis function returns a valid and formatted date from a given milli number. <?php // simple way to avoid magic numbers DEFINE(MILLI, 1000); DEFINE(RPRECISION, 10); DEFINE(DAY_SECONDS, 86400); function date_to_millis($date, $pattern='d.m.Y') { $millisPerDay = DAY_SECONDS*MILLI; list($d, $m, $y) = explode($date, '.'); return micro_to_milli($d*$m*$y*$millisPerDay); } function micro_to_milli($microDate=null) { $date = ($date) ? $date : microtime(); list ($usec, $sec) = explode (' ', $date); return (float) $sec*MILLI + round($usec/MILLI, RPRECISION); } function date_from_millis($millis, $pattern='d.m.Y') { $millis = ($millis) ? $millis : micro_to_milli(); // returns the date using the given pattern or 'd.m.y' by default return date($pattern, $millis/MILLI); } // actual date $date = date('d.m.Y'); var_dump($date); // actual date in milliseconds $milliFromDate = date_to_millis($date); var_dump($milliFromDate); // actual date back from millis $dateFromMilli = date_from_millis($milli); var_dump($dateFromMilli); ?>

« previous php.notes (#171702) next »