PHP,如何将Int值转换为Week days

PHP, how to convert Int value to Week days

为了在数据库中保存星期几,我有现有的代码:

if (isset($_POST['day7'])){$dayOfWeek = 1;} else { $dayOfWeek = ''; }
if (isset($_POST['day1'])){$dayOfWeek = $dayOfWeek + 2;}
if (isset($_POST['day2'])){$dayOfWeek = $dayOfWeek + 4;}
if (isset($_POST['day3'])){$dayOfWeek = $dayOfWeek + 8;}
if (isset($_POST['day4'])){$dayOfWeek = $dayOfWeek + 16;}
if (isset($_POST['day5'])){$dayOfWeek = $dayOfWeek + 32;}
if (isset($_POST['day6'])){$dayOfWeek = $dayOfWeek + 64;}

例如:周一、周五、周六为:int(98) (2+32+64) bin 值 = 1100010

其他例子:周日、周一 = int(3) (2+1) bin 值 = 11

我的问题是:如何反向操作以从 int 值中获取星期几:例如:String(Mon, Fri, Sat)?

我可以用像 1100010 这样的二进制值来做,但是我不明白当二进制值少于 7 个“字符”时如何做,比如 11

<?php
function binToWeekdays($binvalue) {

    $array_week = array();
    $array_week = str_split($binvalue);
    $array_week = array_reverse($array_week);

    $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('1100010');

?>

Returns : 周一、周五、周六,

感谢您的帮助

“str_pad”怎么样? 如果二进制文件少于 7 个“字符”,

str_pad(1,7,"0",STR_PAD_LEFT)

您可以简单地用零填充字符串:

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');

这输出:

Mon, Sun

现场演示:https://3v4l.org/pSl8I

文档:https://www.php.net/manual/en/function.str-pad.php

其他想法(而不是用零填充字符串):

将二进制值转换为整数并使用二进制 &-运算符检查是否选择了日期:

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

此外,如果没有选择星期日,我还使用了一个数组并在返回之前将其内爆以防止尾随逗号。

从 Matthias Radde 那里获取公认的解决方案(我喜欢它是因为使用了二元运算符),但现在能够定义工作日位位置的映射...

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