SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strtotime failing

    print strtotime("17/03/2009"); prints out nothing (im assuming its returning FALSE as specified in the docs)

    so, is it possible to strtotime to accept "17/03/2009"? the php docs dont list this format in their examples but i guess i could convert the numeric month to a string format, but it still doesnt feel right. is this because US and UK have the day and month in different places?
    I need someone to protect me from
    all the measures they take in order to protect me

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stef25 View Post
    is this because US and UK have the day and month in different places?
    Yes, it's looking for m/d/y and 17 is not a valid month, so it can't convert that date. It works fine on "03/17/2009"

    One solution to this is to not allow free-form date input, to use either a JavaScript calendar widget or select lists, so that you never have ambiguity in which field is the month and which is the day.

  3. #3
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im using a jquery datepicker, ill try to set that up to print out US format. but then the client will think its the wrong date. imho a bit of a limitation of this php function. you should be able to pass along some kind of locale variable. php isnt even a US product!
    I need someone to protect me from
    all the measures they take in order to protect me

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's probably something in there that would handle it, maybe in the DateTime package in PHP 5.2.

    You can print it out in the UK format and convert it to the US format in your code before you call strtotime(). Split it on the / characters and rearrange.

  5. #5
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are two quick ways to convert your values to m-d-Y. I was bored and wanted to try and do it in one line.

    PHP Code:
    $input '15/03/2009';

    $timestamp mktime($d explode('/'$input) and 0,0,0$d[1], $d[0], $d[2]);
    $timestamp strtotime(preg_replace('/^(\d+)\/(\d+)\/(\d{4})$/''$2/$1/$3'$input)); 
    Adrian Schneider - Web Developer

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    I believe using dash in place of forward slash changes the parsing to allow day then month.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can change the format the jQuery Datepicker uses.
    Code:
    $( '.selector' ).datepicker( { dateFormat: 'yy-mm-dd' } );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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