5

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.

Tamilvanan
  • 708
  • 1
  • 7
  • 21
  • 1
    timestamp - alco called unixtime it's seconds passed from 1st january 1970 . Seconds !! Not miliseconds. You can't do that using strtotime . – Michał G May 28 '16 at 10:15
  • 1
    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. – Prokhor Sednev May 28 '16 at 10:16
  • 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. – Tamilvanan May 28 '16 at 10:39
  • @John Conde Nobody discuss or ask about milliseconds in date timestamp or microtime in that question (http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php). Then how this question become a duplicate ? – Tamilvanan May 28 '16 at 12:17
  • 1
    @tamilvanan: It is mentioned in the answer posted by John Conde. – Nisse Engström May 28 '16 at 15:01
  • @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 reference – Tamilvanan May 30 '16 at 09:55

3 Answers3

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
Pyton
  • 1,291
  • 8
  • 19
  • Thanks. I got same output for with and without milliseconds. What is the reason ? – Tamilvanan May 28 '16 at 10:21
  • 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) – Tamilvanan May 28 '16 at 10:23
  • I've updated answer. Generally timestamp is Integer value. – Pyton May 28 '16 at 10:43
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 
Prokhor Sednev
  • 668
  • 7
  • 14
  • I am gonna to use your method. I am curious about your regular expression. Can you please tell that, how it works ? – Tamilvanan May 28 '16 at 10:55
  • 1
    ^ - beginning of string, (.+) - capturing group of all symbols, : - symbol ':', (\d+) - capturing group of digits $ - end of string. Pretty simple. I would recommend use site https://regex101.com/ - you can make regexp and test it with ease there. – Prokhor Sednev May 28 '16 at 11:21
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