Chopping and changing a date

Discussion in 'PHP' started by adzeds, Feb 6, 2010.

  1. #1
    I am having a problem converting a date to be used to query my database.

    I am passing the date to my PHP script in this format:
    mm/dd/yyy

    and in order to query my databases date field it needs to be in this format:
    yyyy-mm-dd

    How can I do this with PHP?
     
    adzeds, Feb 6, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    try something as basic as
    $bits = explode('/',$date);
    var_dump($bits); //just so you can see what the explode did. delete once you're up and running.
    $dateymd = $bits[2].'-'.$bits[0].'-'.$bits[1];
    var_dump($dateymd); //just so you can see the NEW variable. delete once you're up and running.
    PHP:
    As a non-american I just can't fathom this whole month-day-year thing. Seems more logical to do small units to big units (british dates) or big units to small (data) - but that's just me :)
     
    sarahk, Feb 6, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    The date() function is for this:

    <?php
    
    $date = date("m/d/y", "1265506687");
    
    $new_date_format = date("y-m-d", $date);
    
    echo $new_date_format;
    
    ?>
    PHP:
     
    danx10, Feb 6, 2010 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    except that his date looks like "02/28/2010" so he needs a step to get it into a timestamp.
     
    sarahk, Feb 6, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    The OP could use strtotime() for the conversion into timestamp.

    <?php
    
    $date = strtotime("02/28/2010");
    
    $new_date_format = date("y-m-d", $date);
    
    echo $new_date_format;
    
    ?>
    PHP:
     
    danx10, Feb 6, 2010 IP
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    hello,

    how can i have a date on this format?

    Sun, 07 February 2010

    thanks
     
    baris22, Feb 7, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    $new_date_format = date("D, jS F Y", $date)
    PHP:
     
    danx10, Feb 7, 2010 IP
  8. hostgenius

    hostgenius Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it amazes me sometimes how ppls DONT use google.

    google "php date" and it would have taken u 2 minutes to find :)

    I have the php date page in my favs as i am working on a booking management script.

    try to always have your date in your script, in the same format as a mysql timestamp, so u can use it in querys with ease
     
    hostgenius, Feb 8, 2010 IP
  9. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #9
    I think I may have an error somewhere else because that is what I am doing but it is not working!

    Will report back with my findings!
     
    adzeds, Feb 8, 2010 IP