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
For get the timestamp in milliseconds you can use
$date = Carbon::now();
$timeInMilliseconds = $date->valueOf()
As a alternate solution
$timeInMilliseconds = $date->getPreciseTimestamp(3)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With