I’m having some headaches with dates in PHP. I’m getting some data from a form and trying to “submit” it with an ajax call to update the web without reloading the page.
I have a datepicker which allows to pick a range of dates and so, I’ve done a dirty function to separate the start and the end date and posting it to PHP separately (in case a range of dates where selected). They are formated in this way: 01/04/2011,08/04/2011
In the PHP so I have the dates variables and try to format them to perform some database queries in this ways:
$date_start = $_POST['date_start'];
$date_start_form = date("Y-m-d", strtotime($date_start));
After some testing I have discovered that if the date is higher to day 12 of whichever month, and then try to format it, it gives me back 1970-01-01.
Does someone know why this happen?
By the way, the way to solve the problem is to not use / as the date separator, if – or . is used like 08.04.2011 or 08-04-2011, then the European method will be assumed to be in use.
If you can only receive dates in 08/04/2011 format, then a simple str_replace will take care of it: