Create 3 digit Millisecond with php
Asked Answered
B

7

9

I have 13 digit number and want to create date and time with include milisecond

Example code is like this this is my php script

$mil = 1328910295939;
$seconds = $mil / 1000;
$showdate = date('Y:m:d H:i:s', $seconds) ;

echo "$showdate";

the result is like this 2012:02:10 15:44:55.xxx ===> xxx is 3 digit miliseconds that i want to show up.

and how to include with 3 digit milisecond after H:i:s

Please help me.....

Barracuda answered 11/2, 2012 at 18:28 Comment(0)
F
10

How about something like this?

$mil = 1328910295939;

function toTimestamp($milliseconds)
{
    $seconds = $milliseconds / 1000;
    $remainder = round($seconds - ($seconds >> 0), 3) * 1000;

    return date('Y:m:d H:i:s.', $seconds).$remainder;
}

echo toTimestamp($mil);

Tadaa!

It should be pretty quick too.

Also, this is the output: 2012:02:10 15:44:55.939 - why you're not using - for delimiting the date portion beats me.

Fritzie answered 11/2, 2012 at 20:39 Comment(4)
Thanks dude..... great work. u are the best, because iam searching for a week in internet does not have solution.Barracuda
Hello westie i had still problem the HOUR time was 2 hour faster... example 22:07:45.434 an HOURS should be 20.Barracuda
That's a problem with your server - you'll need to set the timezone.Fritzie
@Fritzie Should left pad $remainder with zeroes if it has fewer than 3 digits. For example, if $mil = 1328910295039, the output should be 2012:02:10 21:44:55.039, not 2012:02:10 21:44:55.39.Gratt
S
5

Just trim off the last two characters:

substr(date('Y-m-d H:i:s.u',1328910295939), 0, -2)
Sandstorm answered 11/2, 2012 at 18:31 Comment(2)
On my system, this evalutes to "2025-12-10 22:17:55.0000" (PHP 5.3.6 on Ubuntu)Tommietommy
this is my php script $mil = 1328910295939; $seconds = $mil / 1000; $showdate = date('Y:m:d H:i:s', $seconds) ; echo "$showdate"; the result is like this 2012:02:10 15:44:55 and how to include with 3 digit milisecond after H:i:sBarracuda
F
4

Here's a function that will do it for you accurately (by rounding, not cutting off):

function getTimestamp()
{
        $microtime = floatval(substr((string)microtime(), 1, 8));
        $rounded = round($microtime, 3);
        return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded));
}

Explanation:

microtime() returns 2 numbers as 1 string, delimited by a space. the 2nd number is the amount of seconds since the unix epoch, and the 1st number is the amount of microseconds since the 2nd number. Basically, the first number is the amount of microseconds expressed in a 8 precision format (0.00000000) and trailing 0s are never cut off.

We round this to a precision of 3 (0.00), and cut off the leading 0, and append that to the actual timestamp.

For some reason the php doc for u, microseconds, doesn't seem to be actually supported. I get 0.000 everytime when using that method. So I resorted to microtime() as a backup solution.

Fatback answered 11/2, 2012 at 18:50 Comment(2)
Example code is like this this is my php script $mil = 1328910295939; $seconds = $mil / 1000; $showdate = date('Y:m:d H:i:s', $seconds) ; echo "$showdate"; the result is like this 2012:02:10 15:44:55.xxx ===> xxx is 3 digit miliseconds that i want to show up.Barracuda
The u format is supported for dates that contain microseconds (e.g. DateTime instances). The date() function only works with integer timestamps so does not know about fractional seconds.Matteo
A
1
$t = 1328910295939;
echo date('Y-m-d H:i:s.', substr($t, 0, -3)) . substr($t, -3);

Output: 2012-02-10 16:44:55.939 (it depends on the timezone)

Azobenzene answered 11/2, 2012 at 21:28 Comment(0)
D
0

Because these answers were all quite amusing in their complexity, here's yet another answer for future posterity that uses the asker's original code and doesn't treat numbers as strings.

$mil = 1328910295939;
$seconds = floor($mil / 1000);
$fraction = $mil % 1000;
$showdate = date('Y:m:d H:i:s',$seconds) . ".$fraction";

echo "$mil<br>
$seconds<br>
$fraction<br>
$showdate";

Outputs the following on a server set to the EST time zone:

1328910295939
1328910295
939
2012:02:10 16:44:55.939
Darwin answered 9/10, 2012 at 14:27 Comment(0)
T
0

Since I can't add a comment to the of @westie, and if anyone need this, I allow myself to add the missing line to his function for decimals < 100 :

    $seconds = $milliseconds / 1000;
    $remainder = round($seconds - ($seconds >> 0), 3) * 1000;
    $remainder = sprintf("%03d", $remainder);
    return gmdate('H:i:s.', $seconds).$remainder;

Note that I also use gmdate to prevent time zone issues (i suppose you would work on milliseconds for duration calculation and not date calculation)

Tallboy answered 11/1, 2023 at 17:55 Comment(0)
B
0

Since PHP 5.2.0 you can use the DateTime() class:

$date = new DateTime(null, new DateTimeZone('Asia/Dhaka'));
echo $date->format('Y-m-d H:i:s.v')

Output: 2024-02-15 15:42:51.034

Barabarabarabas answered 15/2 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.