SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert string to date

    Hello -

    Does anyone know of an easy way to convert

    "Tuesday, August, 12th" a string to the mysql equivalent plus append the year of 2009. so it looks like this: 2009-08-12

    I don't think strtotime will work for this.

    I am getting this data from a csv, and it has the dates stored this way instead of a mysql friendly way. Any ideas appreciated.

    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well, no, it's possible, but August the 12th 2009 isn't a Tuesday.

    PHP Code:
    <?php
    echo date(
        
    'Y-m-d',
        
    strtotime(
            
    str_replace(
                
    ',',
                
    null,
                
    'Tuesday, August, 18th'
            
    )
        )
    );
    #2009-08-18
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    As of PHP 5.3, you could also use the following: (split onto new lines for forum readability)
    PHP Code:
    $datetime DateTime::createFromFormat(
        
    "l, F, jS",
        
    "Tuesday, August, 12th"
    );

    // 2009-08-12
    echo $datetime->format('Y-m-d'); 
    Off Topic:


    There was a Tuesday, August 12th last year.
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    DateTime::createFromFormat 
    Now that, has been a long time coming.

    Thanks, one day I'll read the docs instead of learning RegExp and DateTime from the school-o-salathe.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •