1

I have a jquery function which passes the date '27/05/2016 11:25 PM' to a PHP file. The PHP file will update this to the database.

I'm using strtotime to convert this string to date format '05/27/2016 11:25 PM' but strtotime returns false.

My PHP date conversion:

$EndDate = strtotime($Date);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);

var_dump($NewEndDateValue); //this returns false
dk007
  • 79
  • 2
  • 9
  • 1
    then `$Date` failed. – Funk Forty Niner May 18 '16 at 18:27
  • 3
    If you're passing a date with `/` separators to strtotime(), then PHP treats it as US format (m/d/Y) ___as described in the [PHP documentation](http://www.php.net/manual/en/datetime.formats.date.php)___, and there is no month 27 in the calendar, so you will get a false returned because it is invalid – Mark Baker May 18 '16 at 18:30
  • 3
    *Let the guesswork games begin* - The ***less*** the good people know, the ***more time*** it takes to provide you with a solution. Which in turn, you're asking us to take a blind shot at an invisible target. – Funk Forty Niner May 18 '16 at 18:31
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 18 '16 at 18:32
  • I am in, sounds like fun @Fred-ii- – RiggsFolly May 18 '16 at 18:32
  • [*I've my money on this......*](http://stackoverflow.com/questions/37307467/php-strtotime-returns-false#comment62136384_37307467) @RiggsFolly you want in on 50 to 1 odds? – Funk Forty Niner May 18 '16 at 18:32
  • deleted answer http://stackoverflow.com/a/37307500/ I guess he/she didn't want to continue with a deeper bore into that rabbit hole. Poor guy/gal. – Funk Forty Niner May 18 '16 at 18:36
  • You see that 4th comment up there? There's a reason I put it there. So, why don't you post your real/full code and put all the guesswork out of things. Now that would be a novel idea, don't you think? ;-) – Funk Forty Niner May 18 '16 at 18:37
  • @Fred-ii- I was very restrained, I commented, but didn't downvote or anything like that – Mark Baker May 18 '16 at 18:39
  • @MarkBaker I usually like target shooting at midnight, especially when I don't know where the true target is. I guess it makes it all that much more exciting. – Funk Forty Niner May 18 '16 at 18:41

1 Answers1

4

strtotime() by default treats dates with / seperators as the wierd USA format for dates, where they start in the middle of a date and work outwards from there (go figure). Its fine to speak it that way but totally illogical to expect a logic machine (computer) to work that way.

Anyway, all you need to do is convert the / to a - and date() will assume a logical date format and therefore work.

<?php
$Date = '27/05/2016 11:25 PM';
$dat = str_replace('/', '-', $Date);
$EndDate = strtotime($dat);

$NewEndDateValue = date('m/d/Y h:i A', $EndDate);

var_dump($NewEndDateValue); // "05/27/2016 11:25 PM"
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149