note 97441 added to function.date
| From: | fabiocbalbuquerque at gmail dot com | Date: | Tue, 20 Apr 2010 05:05:43 +0000 |
| Subject: | note 97441 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168436@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 187.64.38.100
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/97441
Del: integrated -- https://master.php.net/note/delete/97441/integrated
Del: useless -- https://master.php.net/note/delete/97441/useless
Del: bad code -- https://master.php.net/note/delete/97441/bad+code
Del: spam -- https://master.php.net/note/delete/97441/spam
Del: non-english -- https://master.php.net/note/delete/97441/non-english
Del: in docs -- https://master.php.net/note/delete/97441/in+docs
Del: other reasons-- https://master.php.net/note/delete/97441
Reject -- https://master.php.net/note/reject/97441
Search -- https://master.php.net/manage/user-notes.php