0

How can I convert 01/01/2020-15:14 into milliseconds? It is driving me crazy. I tried using "d/m/Y-H:i" but I just can't get an output of milliseconds.

toshi
  • 11
  • 2
  • What do you mean "miliseconds"? Since when? – namln-hust May 27 '22 at 02:42
  • Convert to seconds, then multiply by 1000. Look into `mktime()` for conversion to seconds. https://www.php.net/manual/en/function.mktime.php – mardubbles May 27 '22 at 02:50
  • try `strtotime("01/01/2020-15:14") * 1000`, check this post may helps you https://stackoverflow.com/questions/1532534/converting-human-friendly-date-to-milliseconds – Siddharth Rathod May 27 '22 at 04:57
  • Thank you! Sadly it does not work when I put in my $datearray instead of the string. It just shows 0. The array prints the same date strtotime($arr[1]) – toshi May 27 '22 at 14:33

1 Answers1

0

You should be able to work with the DateTime class, which provides some interesting features. Note: PHP version >= 5.3

Use $date->format('Uv');

<?php
   $date = new DateTime('01/01/2020-15:14');
   $milliseconds = $date->format('Uv');
   echo $milliseconds;
?>
subodhkalika
  • 1,964
  • 1
  • 9
  • 15
  • Thank you so much for your answer. Still have a problem. I get the date out of a array. But when I put: $date = new DateTime($my_dt); in I get an error that I need a datetime object. So I tried. $date = DateTime::createFromFormat('m/d/Y-H:i', $datefromarr[1]); but this gives me the current datetime in ms. – toshi May 27 '22 at 14:09
  • $date = new DateTime("22/02/2022-12:12"); does not work $date = new DateTime("02/22/2022-16:15"); does work How do I get the first one working? – toshi May 27 '22 at 14:38
  • Okay fixed it. The array string had a newline at the end, which I could only see with var_dump(). FIxed it with: str_replace("\n", "", $arr[1]); – toshi May 27 '22 at 15:51