Curtis
Curtis

Reputation: 2704

Date not producing correct result

I'm trying to produce a timestamp to the closest millisecond using PHP, I notice in a JavaScript app that I'm trying to replicate they're generating a string like such:

2019-10-18T18:50:38.699Z

However I'm trying to do the same using this:

public function timestamp()
{
    return date('Y-m-d\TH:i:s') . '.' . date('v') . 'Z';
}

But I'm getting results like this:

2019-10-18T14:51:14.000Z

Upvotes: 0

Views: 39

Answers (1)

Lucas Arbex
Lucas Arbex

Reputation: 909

You should use DateTime instead of date() in order to support microseconds, as @kerbholz brilliantly pointed out on the comments. Try the following, please:

$date = new DateTime();
echo $date->format('Y-m-d H:i:s\.v\Z');

Upvotes: 2

Related Questions