display sequence of days

function weekendDaysToString($days, $lang = 'en') {
    $and = array('pt'=>'e', 'en'=>'and');
    $strWeek = array(     // config with more langs!
        'pt'=>array("Segunda", "Terça", "Quarta", "Quinta", "Sexta",
            "Sábado", "Domingo"),
        'en'=> array("Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday")
    );
    $days = array_unique($days);
    sort($days);
    $seq = preg_replace_callback(  // Split sequence by ",":
        '/0246|024|025|026|135|146|246|02|03|04|05|06|13|14|15|16|24|25|26|35|36|46/',
        function ($m) { return join( ',' , str_split($m[0]) ); },
        implode('',$days)
    );
    // split two or more days by "-":
    $seq = preg_replace('/(\d)\d*(\d)/', '$1-$2', $seq);
    $a = explode(',',$seq);
    $last = array_pop($a);
    $n = count($a);
    // Formatting and translating:
    $seq = $n? implode(", ",$a): $last;
    if ($last && $n) $seq = "$seq $and[$lang] $last";
    return preg_replace_callback(
        '/\d/',
        static function ($m) use (&$strWeek,$lang) {
            return $strWeek[$lang][$m[0]];
        },
        $seq
    );
}



############## TESTINGS #################
print "\n".weekNumbers_toStr(array(6,1,2,3,6),'en'); // corrects order and dups
print "\n".weekNumbers_toStr(array(0,1,2,3,6));      // Monday-Thursday and Sunday

print "\n".weekNumbers_toStr(array(3,4,6),'pt');  // Quinta-Sexta e Domingo
print "\n".weekNumbers_toStr(array(3,4,6));       // Thursday-Friday and Sunday

print "\n".weekNumbers_toStr(array(2,3,4,6));  // Wednesday-Friday and Sunday
print "\n".weekNumbers_toStr(array(3,5));      // Thursday and Saturday
print "\n".weekNumbers_toStr(array(0,2,4,6));  // Monday, Wednesday, Friday and Sunday
print "\n".weekNumbers_toStr(array(0));        // Monday
print "\n".weekNumbers_toStr(array()); 

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source