Convert this string to timestamp PHP

user2876368 picture user2876368 · Oct 13, 2013 · Viewed 95.6k times · Source

I have this string: "13/10 15:00" and I would like to convert it to timestamp but when I do this:

      $timestamp = strtotime("13/10 15:00");

It returns an empty value.

Answer

user1864610 picture user1864610 · Oct 13, 2013

In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error.

If you want to parse a date string with a custom format, it's better to use DateTime::createFromFormat() instead:

$dtime = DateTime::createFromFormat("d/m G:i", "13/10 15:00");
$timestamp = $dtime->getTimestamp();