By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Recurring days of week: permutations and bitwise operations.

P: n/a
I am creating a scheduling application which will allow users to
schedule recurring tasks, e.g., the user can set a task to run (M,T,W)
or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and
use a bitwise OR and AND to determine which days the user selected
(and all the permutations):

const None = 0x0;
const Sunday = 0x1;
const Monday = 0x2;
const Tuesday = 0x4;
const Wednesday = 0x8;
const Thursday = 0x10;
const Friday = 0x20;
const Saturday = 0x40;

If the user selects (Sunday(0000 0001), Monday (0000 0010), Wednesday
(0000 1000) the bits set would be: 0000 1011 = "decimal 11"

How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?
Aug 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
br********@gmail.com nous disait :
How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?
Here's a dirty way. You probably have tens of nicer ways that escape
me right now.

$str = ( ($value & 1)?"Monday ":"" )
.( ($value & 2)?"Tuesday ":"" )
.( ($value & 4)?"Wednesday ":"" )
...

Cheers,

Nico
--
Nicolas - 06 20 71 62 34 - http://nicolas.bouthors.org/album/
Aug 14 '08 #2

P: n/a
On Aug 14, 10:25 am, bretonl...@gmail.com wrote:
I am creating a scheduling application which will allow users to
schedule recurring tasks, e.g., the user can set a task to run (M,T,W)
or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and
use a bitwise OR and AND to determine which days the user selected
(and all the permutations):

const None = 0x0;
const Sunday = 0x1;
const Monday = 0x2;
const Tuesday = 0x4;
const Wednesday = 0x8;
const Thursday = 0x10;
const Friday = 0x20;
const Saturday = 0x40;

If the user selects (Sunday(0000 0001), Monday (0000 0010), Wednesday
(0000 1000) the bits set would be: 0000 1011 = "decimal 11"

How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?
use an array() ("0001" ="Sunday Sunday Sunday", yada yada)

or just use the date object functionability , or use decimal, why
convert back in forth? make more code.
Aug 14 '08 #3

P: n/a
You might be better of storing the information as a time reference,
use can extract the day information from there by doing date_create.
This will help if you want to run it on a specific day as you have the
reference for it, you could go further if you wanted to add the time.

looking mktime()
Aug 14 '08 #4

P: n/a
On Aug 14, 7:25*am, bretonl...@gmail.com wrote:
I am creating a scheduling application which will allow users to
schedule recurring tasks, e.g., the user can set a task to run (M,T,W)
or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and
use a bitwise OR and AND to determine which days the user selected
(and all the permutations):

* * const None = 0x0;
* * const Sunday = 0x1;
* * const Monday = 0x2;
* * const Tuesday = 0x4;
* * const Wednesday = 0x8;
* * const Thursday = 0x10;
* * const Friday = 0x20;
* * const Saturday = 0x40;

If the user selects (Sunday(0000 0001), Monday (0000 0010), Wednesday
(0000 1000) the bits set would be: 0000 1011 = "decimal 11"

How do I decode 0000 1011 back to "Sunday, Monday, Wednesday"?
Looks like I am going to answer my own questions (Thanks to David for
some clarification on bitwise operations).

<?php

/**
* Encode
*/
define("Sunday",0x1);
define("Monday",0x2);
define("Tuesday",0x4);
define("Wednesday",0x8);
define("Thursday",0x10);
define("Friday",0x20);
define("Saturday",0x40);

// Sunday = 1, Monday = 2
// will echo, "is Sunday, is Monday."
$encoded = 3;
/**
* Decode
*/
$isSunday = $encoded & Sunday;
$isMonday = $encoded & Monday;
$isTuesday = $encoded & Tuesday;
$isWednesday = $encoded & Wednesday;
$isThursday = $encoded & Thursday;
$isFriday = $encoded & Friday;
$isSaturday = $encoded & Saturday;

if($isSunday==Sunday) echo "is Sunday \n";

if($isMonday==Monday) echo "is Monday \n";

if($isTuesday==Tuesday) echo "is Tuesday \n";

if($isWednesday==Wednesday) echo "is Wednesday \n";

if($isThursday==Thursday) echo "is Thursday \n";

if($isFriday==Friday) echo "is Friday \n";

if($isSaturday==Saturday) echo "is Saturday \n";
?>
Aug 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.