linccce

enhanced all locale weekday names function

Nov 11th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. function get_list_of_weekdays_in_locale(){
  2.  
  3. // let's remember the current local setting
  4.     $oldLocale = setlocale( LC_TIME, '0' );
  5.  
  6. // initialize out result array
  7.     $localizedWeekdays = array();
  8.  
  9.     $locales = explode(PHP_EOL,shell_exec('locale -a'));
  10.  
  11.     foreach( $locales as $locale ) {
  12.  
  13.         $preferred_locale = setlocale(LC_TIME, $locale);
  14.  
  15.         $localizedWeekdays[$preferred_locale] = array();
  16.  
  17.         // 7 days in a week
  18.         for ( $i = 0; $i < 7; $i ++ ) {
  19.             // set the locale on each iteration again
  20.             setlocale( LC_TIME, $locale );
  21.  
  22.             // combine strftime() with the nifty strtotime()
  23.             $localizedWeekdays[ $locale ][] = strftime( '%A', strtotime( 'next Monday +' . $i . ' days' ) );
  24.  
  25.             // reset the locale for other threads, as a courtesy
  26.             setlocale( LC_TIME, $oldLocale );
  27.         }
  28.  
  29.     }
  30.  
  31. // there is your result in a multi-dimensional array
  32.     return $localizedWeekdays;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment