-1

I have this string

$chktodate = "15/02/2014 9 am";

And when I am doing this

$timetocheck = strtotime($chkdt);

It gives me nothing.

Is it not that the function is supposed to take the string like the format which I mentioned ie 15/02/2014 9 am.

Prithviraj Mitra
  • 11,002
  • 13
  • 58
  • 99
  • 1
    @Glavić: I don't see how that's a duplicate of this question. In this question, the OP has a string which isn't recognized by `strtotime()` as a valid date format and hence returns `FALSE`. The OP asks how to fix this issue, while the other question discusses about working with future dates. – Amal Murali Feb 15 '14 at 12:55
  • possible duplicate of [PHP: strtotime returns "nothing" of type "boolean"](http://stackoverflow.com/questions/19164923/php-strtotime-returns-nothing-of-type-boolean) – Ja͢ck Mar 02 '14 at 06:42

1 Answers1

3

strtotime() doesn't understand that format, and hence returns FALSE. The list of formats recognized by the parser are listed here.

Use DateTime::createFromFormat() to parse this format:

$dateObj = DateTime::createFromFormat('d/m/Y g a', $chktodate);
echo $dateObj->format('Y-m-d H:i:s'); // 2014-02-15 09:00:00

For a list of all the available formatting options, see the documentation.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150