'PHP, how to convert Int value to Week days
For saving the days of week in a database, I've the existing code :
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;}
For exemple : Monday, Friday, Saturday is : int(98) (2+32+64) bin value for that = 1100010
Other exemple : Sunday, Monday = int(3) (2+1) Bin value = 11
My question is : How to do the reverse in order to get the days of week in : exemple : String(Mon, Fri, Sat) from the int value ?
I can do it with a binary value like 1100010, but I don't understand how to do it when the binary is less than 7 "characters", like 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 : Mon, Fri, Sat,
Thanks for your help
Solution 1:[1]
other idea (instead of padding the string with zeros):
convert the binary value to an integer and use the binary &-operator for checking if a day is selected:
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
In addition I have used an array and imploded this before returning to prevent a trailing comma if there is no sunday selected.
Solution 2:[2]
Picking up the accepted solution from Matthias Radde (which I liked because of the use of binary operators), but now able to define the map of a weekdays bit position...
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
Solution 3:[3]
How about "str_pad" ? If the binary is less than 7 "characters",
str_pad(1,7,"0",STR_PAD_LEFT)
Solution 4:[4]
You can simply pad the string with zeros:
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');
This outputs:
Mon, Sun
Live demo: https://3v4l.org/pSl8I
Documentation: https://www.php.net/manual/en/function.str-pad.php
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Honk der Hase |
Solution 3 | Li Yong |
Solution 4 |