问题
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?
回答1:
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:
$date = "08/04/2011";
$date = str_replace('/','.',$date); //08.04.2011
echo date('Y-m-d',strtotime($date)); //2011-04-08
回答2:
They are formated in this way: 01/04/2011,08/04/2011
That is probably your problem: If these dates are supposed in April, you are using the european way of writing dates. strtotime expects american dates, so it's probably interpreting the month as the day, and the day of the month (which is also why it breaks if the day is greater than 12).
Wikipedia: Date and time notation in the United States
回答3:
It sounds like you have the day and the month mixed up. When you feed them to strtotime(), it is expecting the month to be the second number, not the first.
回答4:
That is timezone issue. strtotime takes it into account. You may set the correct one with
date_default_timezone_set('Europe/London');
or whichever prior to strtotime call. See the list of supported timezones.
回答5:
As far as I can see strptime is the only php function to accept a date as a string, and a date format, with the returned array you can then generate a timestamp.
eg:
$timeArr = strptime($date_start ,"DD/MM/YY");
$timestamp = mktime(0, 0, 0, $timeArr['tm_mon']+1, $timeArr['tm_mday'], $timeArr['tm_year']);
$date_start_form = date("Y-m-d", $timestamp);
or you could build the string directly, eg:
$timeArr = strptime($date_start ,"DD/MM/YY");
$date_start_form = sprintf(%d-%d-%d, $timeArr['tm_year'], $timeArr['tm_mon']+1, $timeArr['tm_mday']);
Although thinking about it you could just treat the incomming date as a string that needs to be rearranged, and ignore the fact its a date, eg:
$timeArr = explode($date_start ,"/");
$date_start_form = sprintf(%d-%d-%d, $timeArr[2], $timeArr[1], $timeArr[0]);
回答6:
If you use php=>5.3, it might worth looking at DateTime::createFromFormat function. For your case it may be implemented like:
$date = "08/04/2011";
$date_object = DateTime::createFromFormat('d/m/y', $date);
$timestamp = $date_object->getTimestamp();
来源:https://stackoverflow.com/questions/5597833/strange-behaviour-of-my-code-with-strtotime