Converting date (with milliseconds) into timestamp
Asked Answered
A

3

6

I have date format like '25 May 2016 10:45:53:567'.

I want to convert into the time stamp.

strtotime function returns empty.

$date = '25 May 2016 10:45:53:567';
echo strtotime($date); 
// returns empty

When I removed the milliseconds, it's working.

$date = '25 May 2016 10:45:53';
echo strtotime($date);
// returns 1464153353

Please sort out my issue. Thanks in advance.

Arnold answered 28/5, 2016 at 10:2 Comment(6)
timestamp - alco called unixtime it's seconds passed from 1st january 1970 . Seconds !! Not miliseconds. You can't do that using strtotime .Squires
Timestamps are seconds passed from 1 january 1970. They are stored in integers. You cannot store (or convert) milliseconds in timestamps. But you can convert them into two variables.Billon
I got your points @MichałG & Prokthor. I am going to split out my field in to two. One for date with time and another one for milliseconds.Arnold
@John Conde Nobody discuss or ask about milliseconds in date timestamp or microtime in that question (#2168416). Then how this question become a duplicate ?Arnold
@tamilvanan: It is mentioned in the answer posted by John Conde.Homoiousian
@NisseEngström : In that answer, Milliseconds are trimmed from the given date time stamp. That's not my exact answer. But it's a good referenceArnold
B
3

Split string:

$date = '25 May 2016 10:45:53:001';
preg_match('/^(.+):(\d+)$/i', $date, $matches);
echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
echo 'milliseconds: ' . $matches[2] . PHP_EOL;
// timestamp: 1464162353 
// milliseconds: 001 
Billon answered 28/5, 2016 at 10:21 Comment(2)
I am gonna to use your method. I am curious about your regular expression. Can you please tell that, how it works ?Arnold
^ - beginning of string, (.+) - capturing group of all symbols, : - symbol ':', (\d+) - capturing group of digits $ - end of string. Pretty simple. I would recommend use site regex101.com - you can make regexp and test it with ease there.Billon
L
7

Use DateTime:

$date = DateTime::createFromFormat('d M Y H:i:s:u', '25 May 2016 10:45:53:000');
echo $date->getTimestamp();
// 1464165953

// With microseconds
echo $date->getTimestamp().'.'.$date->format('u');
// 1464165953.000000
Lagomorph answered 28/5, 2016 at 10:11 Comment(3)
Thanks. I got same output for with and without milliseconds. What is the reason ?Arnold
With milliseconds $date1 = DateTime::createFromFormat('d M Y H:i:s:u', '25 May 2016 10:45:53:567'); echo $date1->getTimestamp(); // 1464153353 Without milliseconds $date2 = DateTime::createFromFormat('d M Y H:i:s', '25 May 2016 10:45:53'); echo $date2->getTimestamp(); // 1464153353 (In my localtime)Arnold
I've updated answer. Generally timestamp is Integer value.Lagomorph
B
3

Split string:

$date = '25 May 2016 10:45:53:001';
preg_match('/^(.+):(\d+)$/i', $date, $matches);
echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
echo 'milliseconds: ' . $matches[2] . PHP_EOL;
// timestamp: 1464162353 
// milliseconds: 001 
Billon answered 28/5, 2016 at 10:21 Comment(2)
I am gonna to use your method. I am curious about your regular expression. Can you please tell that, how it works ?Arnold
^ - beginning of string, (.+) - capturing group of all symbols, : - symbol ':', (\d+) - capturing group of digits $ - end of string. Pretty simple. I would recommend use site regex101.com - you can make regexp and test it with ease there.Billon
J
2

Use Datetime instead of date and strtotime.

//using date and strtotime
$date = '25 May 2016 10:45:53:000';
echo "Using date and strtotime: ".date("Y-m-d H:i:s.u", strtotime($date)); 

echo "\n";\

//using DateTime
$date = new DateTime();
$date->createFromFormat('d M Y H:i:s.u', '25 May 2016 10:45:53:000');
echo "Using DateTime: ".$date->format("Y-m-d H:i:s.u"); 
// since you want timestamp
echo $date->getTimestamp();
// Output
// Using date and strtotime: 1969-12-31 16:00:00.000000
// Using DateTime: 2016-05-28 03:25:22.000000

Example

Jeremyjerez answered 28/5, 2016 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.