Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    190
    Thanks
    12
    Thanked 2 Times in 2 Posts

    strtotime fails after the 13th...???

    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
    PHP Code:
    $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?

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by tspek View Post
    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.





    ------
    Last edited by Len Whistler; 01-28-2010 at 10:05 PM.
    Leonard Whistler

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    190
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Len Whistler View Post
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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...ms.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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    tspek (01-28-2010)

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    More on formatting at this comment on the manual page.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    tspek (01-28-2010)

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    190
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    More on formatting at this comment on the manual page.
    So basically if I force the leading zeros it'll work?

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That's what that comment suggests. The formats at fou's link suggest something a little different.
    Are you a Help Vampire?

  • #8
    Regular Coder
    Join Date
    Oct 2009
    Posts
    190
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    That's what that comment suggests.
    And that appears to be the case.

    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •