how to convert date and time..

Discussion in 'PHP' started by MisterLaO, Feb 18, 2010.

  1. #1
    Hi guys.. can any body teach me how to convert date and time formats?

    example if the format of date in the database is 2010-02-12 and i want to convert it to Febuary 12, 2010 to be shown to the users.. and in time, if the format is in 24hrs and i want to change it to 12hrs format..
     
    MisterLaO, Feb 18, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Use strtotime()
     
    danx10, Feb 18, 2010 IP
  3. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    strtotime() will just give you the unix timestamp for the date. You have to use date() to convert the timestamp intot the format you want. So for what you want:
    date("F j, Y",strtotime("2010-03-00"));
    PHP:
     
    superdav42, Feb 18, 2010 IP
  4. MisterLaO

    MisterLaO Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks guys.. got it.. :)
     
    MisterLaO, Feb 18, 2010 IP
  5. MisterLaO

    MisterLaO Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    new problem:
    how can i get the duration of a StartTime variable and an EndTime variable..

    example if starttime is 2pm and endtime is 3:30pm.. how can i get the duration from starttime to endtime? so i'd be able to get the hours and minutes to be stored on different variables?
     
    MisterLaO, Feb 18, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Which is why i said use strtotime().
     
    danx10, Feb 18, 2010 IP
  7. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php

    $start = strtotime('2010-02-18 02:00:00');
    $end = strtotime('2010-02-18 03:30:00');

    $total = $end - $start;

    //Seconds
    echo $total .' seconds. ';
    echo $total/60 . ' minutes. ';
    echo $total/3600 . ' hours. ';


    ?>
     
    orionoreo, Feb 18, 2010 IP