1

I'm creating an application in which user enter a date and make a request. At server side I verify whether user has entered valid date or not . But what the problem comes is PHP isn't converting some of date user enters. For e.g if user selects 13/10/15 with jquery datetimpicker and I print it,

print_r(date('Y-m-d',strtotime($this->request->post['delivery_date'])));

And its showing

1970-01-01

So I tried to check if any error is there or not. To do so I tried

$date = date_parse($this->request->post['delivery_date']);
print_r($date);

And it's showing an error

[error_count] => 1
[errors] => Array
        (
            [0] => Unexpected character
        )

It works fine for other dates but not with some dates. What should I do? Any help will be appreciated.

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Vidhyut Pandya
  • 1,605
  • 1
  • 14
  • 27

2 Answers2

2

Forward slash (/) signifies American M/D/Y formatting, and there isn't a thirteenth month in our calendar.

print_r(date('Y-m-d',strtotime("10/13/15")));

Should be good. Or use DateTime::createFromFormat like

$date = DateTime::createFromFormat('d/m/y', '13/10/15');
echo $date->format('Y-m-d'); // 2015-10-13
Federkun
  • 36,084
  • 8
  • 78
  • 90
0
   $str = '24/12/2013';
   $date = DateTime::createFromFormat('d/m/Y', $str);
   echo $date->format('Y-m-d');
Abhishek kadadi
  • 120
  • 2
  • 11