DateTime/DateTimeZone - Format a timestamp using locale?

Discussion in 'PHP' started by Triexa, Mar 9, 2010.

  1. #1
    So I discovered the wonders of DateTime and DateTimeZone but must have overlooked one critical problem - I cannot see how to use the current locale to format a timestamp!

    For example, today I would like to say its "Mars" (french), not "March".

    How can I do that with these classes?
     
    Triexa, Mar 9, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Use fr_FR as the locale paremeter, when formating the date, or you could also do:

    <?php
    
    $date = date("F", time());
    
    //don't have to use preg_replace - could be done using str_replace (simpler) but i prefer using preg_replace.
    $date = preg_replace('~January~', 'Janvier', $date);
    
    $date = preg_replace('~March~', 'Mars', $date);
    //and continue the months...
    
    echo $date;
    ?>
    PHP:
     
    Last edited: Mar 9, 2010
    danx10, Mar 9, 2010 IP