Datetime to Timestamp in milliseconds in PHP

Hyacinthe picture Hyacinthe · Mar 21, 2016 · Viewed 23.8k times · Source

I would like to create a timestamp in milliseconds from the input '2016-03-22 14:30'. Also the timezone specified should be Australia/Sydney.

I've tried different approaches but none seem to be working.

Can anyone help me please? I'm really struggling with that.

Answer

Kylie picture Kylie · Mar 21, 2016

Pretty self explanatory code, so I wont say much.

date_default_timezone_set('Australia/Sydney'); // set timezone
$yourdate = '2016-03-22 14:30';
$stamp = strtotime($yourdate); // get unix timestamp
$time_in_ms = $stamp*1000;

If you want to display it properly.

echo number_format($time_in_ms, 0, '.', '');