php: convert milliseconds to date
You are already doing it right, 1227643821 is simply not 02-12-2008, it is indeed 25-11-2008.
converting milliseconds to amount of time string
PHP has a date function which do what you want:
date("H:i:s", '183547165');
It outputs:
09:19:25
PHP DATE
Convert milliseconds to minutes: Getting incorrect result
It's because date
takes account of your local timezone. Try using gmdate
instead:
$mil = 4007587;
$timestamp = $mil/1000;
// local timezone
echo date("g:i:s", $timestamp);
// UTC
echo gmdate("g:i:s", $timestamp);
Output
2:06:47
1:06:47
Getting date format m-d-Y H:i:s.u from milliseconds
php.net says:
Microseconds (added in PHP 5.2.2). Note that
date()
will always generate000000
since it takes an integer parameter, whereasDateTime::format()
does support microseconds ifDateTime
was created with microseconds.
So use as simple:
$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";
Recommended and use dateTime()
class from referenced:
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
print $d->format("Y-m-d H:i:s.u"); // note at point on "u"
Note u
is microseconds (1 seconds = 1000000 µs).
Another example from php.net:
$d2=new DateTime("2012-07-08 11:14:15.889342");
Reference of dateTime()
on php.net
I've answered on question as short and simplify to author. Please see for more information to author: getting date format m-d-Y H:i:s.u from milliseconds
Converting milliseconds date to normal date
That timestamp is not in milliseconds so dividing it by 1000 is skewing your date.
$creation_date = new DateTime('@1468102548');
echo $creation_date->format('Y-m-d'); // Outputs 2016-07-09
Demo
PHP Converting milliseconds to date fails for specific millisecond (1425318722000)
It's the fact that you're using the 'U.u'
mask, but the .u
is lost from the value when the result is all 0s after the decimal for that division
for($ms = 1425318721999; $ms <= 1425318722001; ++$ms) {
var_dump(DateTime::createFromFormat('U.u', sprintf('%14.3f', $ms/1000)));
}
Will work, because you're using sprintf() to force those zeroes to be retained after the decimal point
for($ms = 1425318721999; $ms <= 1425318722001; ++$ms) {
var_dump(DateTime::createFromFormat('U', floor($ms/1000)));
}
would also work, but you'd lose the milliseconds precision
By way of some explanation:
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
the createFromFormat()
expects a string as the second argument, so PHP is loose casting the result of your division to a string, and a float value like 1425318722.000
will be cast to a string of "1425318722"
with no decimal point or following zeroes, so it doesn't conform with the U.u
mask which requires the decimal point and following digits
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
Use example :
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
Input can be any supported date and time format.
Output :
4 months ago
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Function :
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Converting date (with milliseconds) into timestamp
Split string:
$date = '25 May 2016 10:45:53:001';
preg_match('/^(.+):(\d+)$/i', $date, $matches);
echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
echo 'milliseconds: ' . $matches[2] . PHP_EOL;
// timestamp: 1464162353
// milliseconds: 001
Related Topics
PHP Startup Unable to Load Dynamic Library PHP_Mongo.Dll
How to Run Array_Filter Recursively in a PHP Array
How to Set Default Value for Form Field in Symfony2
How to Change/Custom Password Field Name for Laravel 4 and Laravel 5 User Authentication
Need a Good Regex to Convert Urls to Links But Leave Existing Links Alone
Force Browser to Download Image with JavaScript Window.Open
How to Load PHP File into Div by Jquery
PHP Remove Duplicate Values from Multidimensional Array
What Does "Mass Assignment" Mean in Laravel
How to Replace Part of a String in PHP
Resize/Crop/Pad a Picture to a Fixed Size