Converting human-friendly date to milliseconds

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

strtotime($human_readable_date) * 1000

Solution 2

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 🙂

Solution 3

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply