+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: PHP date() alternitive

  1. #1
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Question PHP date() alternitive

    I have been working on a site in a different language and because of that, I have to work my way around the date() function. I found this script in php.net but when I run it, it returns all of the numbers but not the words. They appear as a dimond with a question mark in it. I have already made sure everything on the page is UFT-8 and it still is giving me problems. Are some of the functions outdated or what?
    PHP Code:
    <?php
    /*
      these are the russian additional format characters
      д: full textual representation of the day of the week
      Д: full textual representation of the day of the week (first character is uppercase),
      к: short textual representation of the day of the week,
      К: short textual representation of the day of the week (first character is uppercase),
      м: full textual representation of a month
      М: full textual representation of a month (first character is uppercase),
      л: short textual representation of a month
      Л: short textual representation of a month (first character is uppercase),
    */
    function date_ru($formatum$timestamp=0) {
      if ((
    $timestamp <= -1) || !is_numeric($timestamp)) return '';
      
    $q['д'] = array(-=> 'w''воскресенье','понедельник''вторник''среда''четверг''пятница''суббота');
      
    $q['Д'] = array(-=> 'w''Воскресенье','Понедельник''Вторник''Среда''Четверг''Пятница''Суббота');
      
    $q['к'] = array(-=> 'w''вс','пн''вт''ср''чт''пт''сб');
      
    $q['К'] = array(-=> 'w',  'Вс','Пн''Вт''Ср''Чт''Пт''Сб');
      
    $q['м'] = array(-=> 'n''''января''февраля''марта''апреля''мая''июня''июля''августа''сентября''октября''ноября''декабря');
      
    $q['М'] = array(-=> 'n''''Января''Февраля''Март''Апреля''Май''Июня''Июля''Август''Сентября''Октября''Ноября''Декабря');
      
    $q['л'] = array(-=> 'n''''янв''фев''мар''апр''май''июн''июл''авг''сен''окт''ноя''дек');
      
    $q['Л'] = array(-=> 'n''',  'Янв''Фев''Мар''Апр''Май''Июн''Июл''Авг''Сен''Окт''Ноя''Дек');

      if (
    $timestamp == 0)
        
    $timestamp time();
      
    $temp '';
      
    $i 0;
      while ( (
    strpos($formatum'д'$i) !== FALSE) || (strpos($formatum'Д'$i) !== FALSE) || 
              (
    strpos($formatum'к'$i) !== FALSE) || (strpos($formatum'К'$i) !== FALSE) || 
              (
    strpos($formatum'м'$i) !== FALSE) || (strpos($formatum'М'$i) !== FALSE) || 
              (
    strpos($formatum'л'$i) !== FALSE) || (strpos($formatum'Л'$i) !== FALSE)) {
        
    $ch['д']=strpos($formatum'д'$i);
        
    $ch['Д']=strpos($formatum'Д'$i);
        
    $ch['к']=strpos($formatum'к'$i);
        
    $ch['К']=strpos($formatum'К'$i);
        
    $ch['м']=strpos($formatum'м'$i);
        
    $ch['М']=strpos($formatum'М'$i);
        
    $ch['л']=strpos($formatum'л'$i);
        
    $ch['Л']=strpos($formatum'Л'$i);
        foreach (
    $ch as $k=>$v)
          if (
    $v === FALSE)
            unset(
    $ch[$k]);
        
    $a min($ch);
        
    $temp .= date(substr($formatum$i$a-$i), $timestamp) . $q[$formatum[$a]][date($q[$formatum[$a]][-1], $timestamp)];
        
    $i $a+1;
      }
      
    $temp .= date(substr($formatum$i), $timestamp);
      return 
    $temp;
    }

    echo 
    'Сегодня '.date_ru('Д, d л Y');
    ?>

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP date() alternitive

    There is an incompability in the charset used in the php file, the one parsed and the one displayed by the browser. Try adding a htmlentities in your echo function:
    PHP Code:
    echo htmlentities('Сегодня '.date_ru('Д, d л Y')); 
    However, use the utf8_encode function if the results are not sent to a browser.
    Last edited by xav0989; 10-03-2009 at 12:10 AM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: PHP date() alternitive

    PHP Code:
    //I tried:
    echo  htmlentities('Сегодня '.date_ru('Д, d л Y'));
    //and
    echo utf8_encode(htmlentities('Сегодня '.date_ru('Д, d л Y')));
    //but i still got random characters. the code you see above is
    //the only thing I have in the file so charsets can't really be 
    //getting confused 

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP date() alternitive

    check the charset in which the file is saved and the charset that the server is receiving. Try setting it to ISO-8859-1.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: PHP date() alternitive

    In ISO-8859-1, the code returns: Сегодня , 04. 2009
    In UTF-8, it returns Сегодня �, 04. � 2009 (When both the browser and file are in UTF-

    Are you guys sure it is a charset problem? I think it might be something wrong with the code. Could any of it have an error or be outdated?

  6. #6
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP date() alternitive

    I'm pretty sure it's an encoding error. It looks like the UTF-8 is pretty close, there's probably use some minor tweak you have to do yet.

    UTF-8 is supposed to be able to encode any character in any language.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP date() alternitive

    PHP does not handle Unicode well.

    For example:

    PHP Code:
    $test 'Д, d л Y' ;

    echo 
    strlen$test ) ; 
    Yields 20.

    I was going to suggest adding

    PHP Code:
    declare(encoding="utf-8"); 
    at the top of the script, but that feature did not come in until PHP 5.3
    Last edited by descalzo; 10-04-2009 at 08:10 PM.
    Nothing is always absolutely so.

  8. #8
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: PHP date() alternitive

    Thanks for the tip descalzo but I have PHP 5.2.9. I also tried putting the utf8_encode() in front of the arrays but it didn't change anything either.

  9. #9
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP date() alternitive

    Quote Originally Posted by descalzo View Post
    PHP Code:
    $test 'Д, d л Y' ;

    echo 
    strlen$test ) ; 
    Yields 20.
    This is theoretically correct since the characters used are multibyte characters and can take up to four bytes each, though logically it makes less sense.
    Edit:
    Have you tried using heredoc syntax?

    http://www.php.net/manual/en/languag...syntax.heredoc

    it will be inconvenient to change all the quotes, but it may be the solution
    Last edited by garrettroyce; 10-04-2009 at 09:05 PM. Reason: Automerged Doublepost
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  10. #10
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP date() alternitive

    Quote Originally Posted by garrettroyce View Post
    This is theoretically correct since the characters used are multibyte characters and can take up to four bytes each, though logically it makes less sense.
    And that is exactly why this code is broken.

    PHP Code:
    $temp .= date(substr($formatum$i$a-$i), $timestamp) . 
         
    $q[$formatum[$a]][date($q[$formatum[$a]][-1], $timestamp)]; 
    Specifically (there may be other places, this is one I found), where he uses

    PHP Code:
     
    $formatum
    [$a
    He is expecting this to be a multibyte character, so he can use it as a key to the $q hash.
    But PHP only gives him one byte.
    Nothing is always absolutely so.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. php Date()
    By driveflexfuel in forum Programming Help
    Replies: 6
    Last Post: 04-28-2009, 01:25 PM
  3. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers