Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Making function_html_select_date locale aware

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
WanWizard
Smarty n00b


Joined: 22 Nov 2007
Posts: 4

PostPosted: Thu Nov 22, 2007 3:35 pm    Post subject: Making function_html_select_date locale aware Reply with quote

When I was converting our CMS code and templates to support multilingual sites, I noticed that {html_select_date} was not locale aware.

With this change you can correct that (works on PHP without nl_langinfo support).

Add this function:
Code:
function find_date_format() {

   // for *nix platforms, and PHP >= 4.0.7, use nl_langinfo information
   if (version_compare(phpversion(), "4.0.7") != -1 && function_exists("nl_langinfo") && substr(php_os, 0, 3) != "WIN") {
       return str_replace(array('E', 'B'), array('D', 'M'), strtoupper(preg_replace('/[^a-z]/i', '', nl_langinfo(D_FMT))));
   }

   // *** if nl_langinfo is not available, do it the hard way ;-)
   
   // find the date separator
   $date_separator = preg_replace('/[a-zA-Z0-9]/i', '', strftime("%x"));

   // if a separator has been found
   if (!empty($date_separator)) {

      // get the different fields from the date
      $date_fields = explode($date_separator{0}, strftime("%x"));
      // if we found 3 parts in the date string
      if (count($date_fields) == 3) {

         // possible values
         $days = array(strftime("%d"), strftime("%e"));
         $months = array(strftime("%m"), strftime("%b"), strftime("%B"));
         $years = array(strftime("%Y"), strftime("%y"));

         // part 1
         if ($date_fields[0] == $date_fields[1]) {
            // month and date the same, then assume MD if the locale uses am/pm, or DM otherwise
            $format = strftime("%p") == "" ? "D" : "M";
         } elseif (in_array($date_fields[0], $days)) {
            $format = "D";
         } elseif (in_array($date_fields[0], $months)) {
            $format = "M";
         } elseif (in_array($date_fields[0], $years)) {
            $format = "Y";
         }

         // part 2
         if ($date_fields[0] == $date_fields[1]) {
            // month and date the same, then assume MD if the locale uses am/pm, or DM otherwise
            $format = strftime("%p") == "" ? "M" : "D";
         } elseif (in_array($date_fields[1], $days)) {
            $format .= "D";
         } elseif (in_array($date_fields[1], $months)) {
            $format .= "M";
         } elseif (in_array($date_fields[1], $years)) {
            $format .= "Y";
         }

         // part 3
         if (in_array($date_fields[2], $days)) {
            $format .= "D";
         } elseif (in_array($date_fields[2], $months)) {
            $format .= "M";
         } elseif (in_array($date_fields[2], $years)) {
            $format .= "Y";
         }
      }
   }
   
   // return the format detected, or YMD if detection failed
   if (isset($format) && strlen($format) == 3) {
      return $format;
   } else {
      return "YMD";
   }
}

Then find:
Code:
$field_order = "MDY";

and replace it by:
Code:
$field_order = find_date_format();

This code is tested on Linux and Windows, with different versions of PHP4. I haven't had any failures sofar, and you can still use the field_order parameter to override the detection (although I think with this addition, who needs it? Wink)
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP