SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Spanish date

  1. #1
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Spanish date

    No, it's not about going on a date in Spain.

    It's about outputting date strings in the Spanish language. I'm translating a website to Spanish and I could not find a php script which output the date() function in Spanish so I wrote a SpanishDate class. What I don't like about it is that there are two Spanish words often used in date strings (de, del) that have letters used by the date() function so I had to build a work-around for them. I wonder if there is a better way.

    The class (easily localized to any other language):

    PHP Code:
    class SpanishDate {

        private 
    $timeStamp;
        private 
    $d;             // day of the week index 0-6
        
    private $m;             // month of the year index 0-11
        
        
    private $day = array(domlunmarmiejueviesab);
        private 
    $dayFull = array(domingolunesmartesmiercolesjuevesviernessabado);
        private 
    $mon = array(enefebmarabrmayjunjulagosepoctnovdic);
        private 
    $monFull = array(enerofebreromarzoabrilmayojuniojulioagostoseptiembreoctubrenoviembrediciembre);
        
        public function 
    __construct($offset 0) {
            
    $this->timeStamp mktime() + $offset;
            
    $this->gmdate('w'$this->timeStamp);
            
    $this->gmdate('n'$this->timeStamp) - 1;
        }
        
        public function 
    format($format) {
            
    $format str_replace('del''#10#'$format);
            
    $format str_replace('de''#11#'$format);
            
    $format str_replace('D''#1#'$format);
            
    $format str_replace('l''#2#'$format);
            
    $format str_replace('M''#3#'$format);
            
    $format str_replace('F''#4#'$format);
            
    $result gmdate($format$this->timeStamp);
            
    $result str_replace('#1#'$this->day[$this->d], $result);
            
    $result str_replace('#2#'$this->dayFull[$this->d], $result);
            
    $result str_replace('#3#'$this->mon[$this->m], $result);
            
    $result str_replace('#4#'$this->monFull[$this->m], $result);
            
    $result str_replace('#10#''del'$result);
            
    $result str_replace('#11#''de'$result);
            return 
    $result;
        }


    Denny Schlesinger
    web services

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strftime()

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your system has a Spanish locale installed, you can use set_locale to change the current locale and then use strftime to format the time according to the setting. Note that locale settings are broken on Windows.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    New to PHP in extenstion form, planned to be integrated into PHP 6. Currently including with PHP 5.3, downloadable for older versions.

    IntlDateFormatter, "This class represents the ICU date formatting functionality. It allows users to display dates in a localized format or to parse strings into PHP date values..."
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    strftime()
    Thanks! It works but I had to jump through hoops get the time right. Here is the problem:

    My server is in California
    My website is for Venezuela where the time zone is GMT -4:30
    php does not support the Venezuelan time zone (VET)

    This works:

    PHP Code:
    echo ucfirst(strftime("%A %e de %B del %Y - %H:%M:%S"mktime (gmdate("H")-4gmdate("i")-30gmdate("s"), gmdate("n"), gmdate("j"), gmdate("Y")))); 
    or this

    PHP Code:
    echo ucfirst(strftime("%A %e de %B del %Y - %H:%M:%S"mktime (gmdate("H"), gmdate("i"), gmdate("s")-16200gmdate("n"), gmdate("j"), gmdate("Y")))); 
    Thanks!
    Denny Schlesinger
    web services

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    My website is for Venezuela where the time zone is GMT -4:30
    php does not support the Venezuelan time zone (VET)
    You are just looking for the wrong name. In PHP's timezone database it is under "America/Caracas", Caracas is the capital of Venezuela.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    You are just looking for the wrong name. In PHP's timezone database it is under "America/Caracas", Caracas is the capital of Venezuela.
    Caracas is the capital of Venezuela I should know, I live there!

    Now I'm cooking with gas!

    PHP Code:
    setlocale(LC_TIME'es_ES.ISO_8859-1');
    date_default_timezone_set('America/Caracas'); 
    Thanks!
    Denny Schlesinger
    web services


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
  •