Convert date to milliseconds in laravel using Carbon
Asked Answered
H

7

23

i make a date in laravel with carbon

$date = Carbon::createFromDate(2018,02,16);

how should i change it to milliseconds?

something like this:

18:16:30 -> 1532785457060
Horner answered 28/7, 2018 at 13:49 Comment(3)
try this: strtotime($date) * 1000Examination
This is the answer: strtotime($human_readable_date) * 1000 Try to make a search first. if don't found, then, you can put your question. Your question is answered hereTenorrhaphy
Possible duplicate of: #1533034Glutamine
R
15

For get the timestamp in milliseconds you can use

$date = Carbon::now();
$timeInMilliseconds = $date->valueOf()

As a alternate solution

 $timeInMilliseconds = $date->getPreciseTimestamp(3)
Rootless answered 12/1, 2021 at 12:12 Comment(1)
For reference, see GettersVannessavanni
F
12

It works, June, 2022.

now()->getTimestampMs()

// 1654259358879
Foredoom answered 3/6, 2022 at 12:28 Comment(0)
B
7

This works in laravel 5.5 with carbon 1.

$timestamp = (int) round(now()->format('Uu') / pow(10, 6 - 3));

this is actually what getPreciseTimestamp(3) in carbon2 does.

Byrnes answered 29/7, 2020 at 19:5 Comment(1)
getPreciseTimestamp is only available on carbon 2, which laravel 5.5 doesn't have.Byrnes
M
4

You can convert any date. An example is below.

$dateWithMs = '2021-07-30 12:02:07.376000';

$timestamp = (int) round(Carbon::parse($date)->format('Uu') / pow(10, 6 - 3));

You should use Laravel >= 5.5 with Carbon 1.

It is working for me.

Mayhem answered 30/7, 2021 at 9:23 Comment(0)
L
3
>>> $now = now();
=> Illuminate\Support\Carbon @1571283623 {#2987
     date: 2019-10-17 03:40:23.530274 UTC (+00:00),
   }
>>> $now->timestamp
=> 1571283623
>>> $x = $now->timestamp . $now->milli
=> "1571283623530"
>>> \Carbon\Carbon::createFromTimestampMs($x)->toDateTimeString()
=> "2019-10-17 03:40:23"
>>> >>> \Carbon\Carbon::createFromTimestampMs($x)->format('Y-m-d H:i:s.u')
=> "2019-10-17 03:40:23.530000"
Lepidolite answered 17/10, 2019 at 3:44 Comment(0)
T
3

Takamura's answer is very close to being correct, but it contains a bug: you have to left pad the number with zeroes or you'll get the wrong answer if the current milliseconds are less than 100.

This example will give you the current time, in milliseconds:

$carbon = now();

$nowInMilliseconds = (int) ($now->timestamp . str_pad($now->milli, 3, '0', STR_PAD_LEFT));

To explain why you have to left pad the milliseconds a little bit more:

$seconds = 5;
$milliseconds = 75; // milliseconds are always between 0 and 999

// wrong answer: 575
$totalInMs = $seconds . $milliseconds; 

// correct answer: 5075
$totalInMs = $now->timestamp . str_pad($now->milli, 3, '0', STR_PAD_LEFT); 

Torn answered 14/5, 2020 at 18:46 Comment(0)
C
-3

You can do the following

$date = Carbon::createFromDate(2018,02,16); 

// 2018-02-16 15:43:38.617547 Europe/Berlin (+01:00)

$dateInMills = $date->timestamp;

// 1518792294
Charlotte answered 24/4, 2021 at 13:45 Comment(2)
As @Keyur Mistry mentioned timestamp returns time in second, not in millisecondArliearliene
It's the seconds, NOT milliseconds!Plasmolysis

© 2022 - 2024 — McMap. All rights reserved.