Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting human-friendly date to milliseconds [closed]

Tags:

php

datetime

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

like image 710
stunnaman Avatar asked Oct 07 '09 15:10

stunnaman


People also ask

How do you convert Date objects to milliseconds?

Once you have the Date object, you can get the milliseconds since the epoch by calling Date. getTime() . The full example: String myDate = "2014/10/29 18:10:45"; //creates a formatter that parses the date in the given format SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf.

How do you parse a Date to milliseconds?

Approach : First declare variable time and store the milliseconds of current date using new date() for current date and getTime() Method for return it in milliseconds since 1 January 1970. Convert time into date object and store it into new variable date. Convert the date object's contents into a string using date.

How do you get milliseconds in laravel?

Just use timestamp property of Carbon object to get a time in milliseconds.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

strtotime($human_readable_date) * 1000 
like image 102
chaos Avatar answered Sep 29 '22 20:09

chaos


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 :)

like image 28
drAlberT Avatar answered Sep 29 '22 18:09

drAlberT