Converting human-friendly date to milliseconds [closed]
Asked Answered
C

3

34

How to convert human-friendly date to milliseconds since the unix epoch?

Caril answered 7/10, 2009 at 15:55 Comment(3)
dupe https://mcmap.net/q/86893/-how-to-convert-date-to-timestamp-in-phpRestrainer
If you take into account the milliseconds issue it is not a dupeWatcher
It depends, to know that multiply seconds by 1k is not equal to have a milliseconds precision? see my answer :)Watcher
M
58
strtotime($human_readable_date) * 1000
Mesopause answered 7/10, 2009 at 15:57 Comment(3)
+1 for spotting he said milliseconds, which I didn't.Salema
Note that you have milliseconds only as a fake, as your real precision is only of 1k milliseconds, aka second :) ... This way you don't have milliseconds, but seconds expressed as number of millisecondsWatcher
loses microsecond precisionRounded
W
18

Pay attention: strtotime() * 1000 is ok to have seconds expressed as milliseconds!

The right answer is that it is not possible to have a millisecond precision on date/time functions in PHP. The precision of Unix Epoc based functions is only of 1k milliseconds, aka second :)

Using the suggested answers you don't have milliseconds, but seconds expressed as number of milliseconds.

If you are aware of this, and you don't really need a millisecond precision then the answers given are ok, but the question was wrong :)

Watcher answered 7/10, 2009 at 16:17 Comment(0)
S
15

You're looking for strtotime.

Sample Usage:

$myvar = strtotime("7 October 2009");

That gives you seconds since the Unix epoch, so you want:

$myvar = strtotime("7 October 2009") * 1000;

Watch out for the fact that strtotime "guesses" what you mean (how should it interpret "12-08-2009"? probably as 8th December, but it might equally validly - and being a Brit, thoroughly sensibly - guess 12th August). If you know the format in advance, use strptime.

Salema answered 7/10, 2009 at 15:56 Comment(1)
This is a terrific answer as it also goes into detail on the date formatting. +1Antidepressant

© 2022 - 2024 — McMap. All rights reserved.