How to convert human-friendly date to milliseconds since the unix epoch?
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.
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.
Just use timestamp property of Carbon object to get a time in milliseconds.
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.
strtotime($human_readable_date) * 1000
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 :)
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