storm
storm

Reputation: 409

Date to milliseconds in PHP

It maybe the simplest question but I'm struggling with this. I want to get the current time to millisecond for example 2016-03-04 11:57:00,00 without separators 2016030411570000

I've tried this date("YmdHisu") but it returns time to microseconds.

Upvotes: 1

Views: 19621

Answers (4)

Pooja Gupta
Pooja Gupta

Reputation: 51

$date = new DateTime();
$dateTime = $date->format('YmdHisv');

Upvotes: 0

Karolis
Karolis

Reputation: 2618

How about:

$date = (new DateTime())->format('YmdHisv');

Upvotes: 0

axiac
axiac

Reputation: 72226

I've tried this date("YmdHisu") but it returns time to microseconds.

If you don't need the microseconds then just strip them:

substr(date("YmdHisu"), 0, -3);

But, because currently date() formats the value returned by time() that doesn't contain fractions of seconds, the result will always end with 000 (no milliseconds available).

You can get the date and the milliseconds as you need if you use date("YmdHis") to format the date and time part (whole seconds) and extract and concatenate the first 3 digits of the microseconds part returned by microtime():

echo(date("YmdHis").substr(microtime(FALSE), 2, 3));

Upvotes: 3

Charly H
Charly H

Reputation: 167

like this?

echo strtotime("2016-03-04 11:57:00") * 1000;

Output:

1457089020000 

Upvotes: 2

Related Questions