Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chron's avatar
Level 6

Is there a way to convert a full date to milliseconds?

Is there a way to convert a now() to milliseconds?

0 likes
5 replies
adamprickett's avatar

Milliseconds since when? You can get the value as an Unix timestamp using now()->timestamp

tinfoilman's avatar

Use microtime(true) instead of now() to get the timestamp in milliseconds.

Chron's avatar
Level 6

For example, I got this

date: 2020-07-06 20:17:28.900295

I want to convert the whole date to milliseconds or something that can be compared to another date like that.

tykus's avatar
tykus
Best Answer
Level 104

Use microtime(true) instead of now() to get the timestamp in milliseconds.

No., this returns in seconds with milliseconds (as a decimal value). With both solutions given above, the OP would need times 1000 the value returned because they both return seconds

$millisecondsSinceEpoch = microtime(true) * 1000

Edit @chron Carbon has a getPreciseTimestamp method which takes a precision argument (3 give millisecond precision):

$numMilisecondsSinceEpoch = Carbon::parse('2020-07-06 20:17:28.900295')->getPreciseTimestamp(3);
adamprickett's avatar

If you need to compare date/times, Carbon has this built-in:

now()->gte(Carbon::parse('2020-01-01 01:02:03'));
// true

now()->lt(Carbon::parse('2020-01-01 01:02:03'));
// false

Please or to participate in this conversation.