...

View Full Version : strtotime fails after the 13th...???



tspek
01-28-2010, 09:40 PM
So I've got a form in which the user enters a date as 1-30-2010.

The dates in my database are 2010-1-30.

So i'm using strtotime to convert the date. In double checking that it's working like I need it to, I am echoing both formats as well as the strtotime converstion to make sure everything is wroking.

It works fine until you go beyond the 12th of any month, at which point the strtotime is empty (false, i presume) which then makes the converted date go to december 31st 1969.

Am I missing something here?

Here is the PHP

$from_date = $_GET['from_date'];
echo "$from_date<br>";
$convert = strtotime($from_date);
echo "$convert <br>";
$fix = date('Y-m-d', $convert);
echo $fix;

For example, if a user selects January 5th, 2010 the result that's echoed is:

1-5-2010 ($from_date = $_GET['from_date'];
1272693600($convert = strtotime($from_date);
2010-5-01 ($fix = date('Y-m-d, $convert);

But if I select any date of any month beyond the 12th it doesn't work, for example if a user selects January 13th, 2010 the result that's echoed is:

1-22-2010($from_date = $_GET['from_date'];
*EMPTY*($convert = strtotime($from_date);
1969-12-31($fix = date('Y-m-d, $convert);


What's going on here?

Len Whistler
01-28-2010, 10:02 PM
So I've got a form in which the user enters a date as 1-30-2010.

The dates in my database are 2010-1-30.

I would prefer to have the user enter as little info as possible. In this case a PHP generated drop down select menu with dates would be easy to use and they can't enter the date in the wrong format.





------

tspek
01-28-2010, 10:08 PM
I would prefer to have the user enter as little info as possible. In this case a PHP generated drop down select menu with dates would be easy to use and they can't enter the date in the wrong format.

------

That's more an issue of form validation in which case a javascript function that allows the user to select the date from a pop-up calender verifies the right format and a javascript form validation verifies that the format is the right combination of numbers and dashes.

From there there will be a server-side validation.

The issue here isn't a validation issue as the format is correct and the conversion works up to a point.

FWIW
The other times in which I have to have a user save a date, currently I am using the method you mention (though the intent is to change it to what i'm trying to do here). I don't like how it looks and it makes more work for the user.

Fou-Lu
01-28-2010, 10:36 PM
There is a reason for the 13th. The format is improper, there is no 13th month.
Here is a link to the GNU date input page: http://www.gnu.org/software/automake/manual/tar/Calendar-date-items.html#SEC116

You're format has to match a specification on this site in order to be usable in strtotime. There is only one combination for hyphenated entry:


day-month-year

tomws
01-28-2010, 10:40 PM
More on formatting at this comment (http://us3.php.net/manual/en/function.strtotime.php#91165) on the manual page.

tspek
01-28-2010, 10:42 PM
More on formatting at this comment (http://us3.php.net/manual/en/function.strtotime.php#91165) on the manual page.

So basically if I force the leading zeros it'll work?

tomws
01-28-2010, 10:45 PM
That's what that comment suggests. The formats at fou's link suggest something a little different.

tspek
01-28-2010, 10:51 PM
That's what that comment suggests.

And that appears to be the case.

Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum