PHP Snippet 1:
function binToWeekdays($binvalue) {
$decvalue = bindec($binvalue);
$weekdays = array();
if($decvalue & 1 << 1){
$weekdays[] = 'Mon';
}
if($decvalue & 1 << 2){
$weekdays[] = 'Tue';
}
if($decvalue & 1 << 3){
$weekdays[] = 'Wed';
}
if($decvalue & 1 << 4){
$weekdays[] = 'Thu';
}
if($decvalue & 1 << 5){
$weekdays[] = 'Fri';
}
if($decvalue & 1 << 6){
$weekdays[] = 'Sat';
}
if($decvalue & 1 << 0){
$weekdays[] = 'Sun';
}
return implode(', ', $weekdays);
}
echo binToWeekdays('1100010') . "<br />\n"; // => Mon, Fri, Sat
echo binToWeekdays('11') . "<br />\n"; // => Mon, Sun
PHP Snippet 2:
function binToWeekdays($binvalue, $map) {
$decvalue = bindec($binvalue);
$weekdays = [];
foreach($map as $day => $exp){
if (($decvalue & (1 << $exp)) != 0) {
$weekdays[] = $day;
}
}
return implode(', ', $weekdays);
}
// with sunday = 0
$map = [
'Sun' => 0,'Mon' => 1,'Tue' => 2,
'Wed' => 3,'Thu' => 4,'Fri' => 5,'Sat' => 6,
];
echo binToWeekdays('1100010', $map) . "<br />\n"; // => Mon, Fri, Sat
echo binToWeekdays('11', $map) . "<br />\n"; // => Mon, Sun
// with monday = 0
$map = [
'Mon' => 0,'Tue' => 1,'Wed' => 2,
'Thu' => 3,'Fri' => 4,'Sat' => 5,'Sun' => 6,
];
echo binToWeekdays('1100010', $map) . "<br />\n"; // => Tue, Sat, Sun
echo binToWeekdays('11', $map) . "<br />\n"; // => Mon, Tue
PHP Snippet 3:
str_pad(1,7,"0",STR_PAD_LEFT)
PHP Snippet 4:
function binToWeekdays($binvalue) {
$binvalue = str_pad($binvalue, 7, "0", STR_PAD_LEFT);
$array_week = array_reverse(str_split($binvalue));
$weekdays = '';
if ($array_week[1] == 1) {
$weekdays .= 'Mon, ';
}
if ($array_week[2] == 1) {
$weekdays .= 'Tue, ';
}
if ($array_week[3] == 1) {
$weekdays .= 'Wed, ';
}
if ($array_week[4] == 1) {
$weekdays .= 'Thu, ';
}
if ($array_week[5] == 1) {
$weekdays .= 'Fri, ';
}
if ($array_week[6] == 1) {
$weekdays .= 'Sat, ';
}
if ($array_week[0] == 1) {
$weekdays .= 'Sun';
}
return $weekdays;
}
echo binToWeekdays('11');