Go Down

Topic: Finding an odd (or even) day of month for watering (Read 553 times) previous topic - next topic


It's been suggested I share this as it may be useful to others.  In my area we may only use irrigation if the day of month is odd and your house number is odd (or both are even).  I use arduino to manage the automation, but PHP to do the scheduling logic.  The following is the simplest way I have found to test the day of month (in PHP, but the principle works).  As shown, it returns True if the day of month is odd - my house number is odd so I can water on odd days.  Swap True and False if you want it to return True on even days of month. 

$oddDay = (2*intval(date('d')/2)==date('d')) ? False : True;

It works by dividing the day of month by 2, then discarding the remainder if any, and multiplying the integer by 2.  If the result equals the day of month, then it is an even day.  If the result does not equal the day of month then the day of month is odd,
e.g: dom = 29; 29/2 = 14.5, integer 14 x 2 = 28; (28 != 29, so odd day).   
       dom = 26; 26/2 = 13, integer 13 x 2 =26; (26 = 26, so even day).

I know it's in PHP, but hope it helps someone.  I'm sure it would be easy to implement in C++. The comparison uses the ternary operator, which is very useful and worth learning about. 


Jun 16, 2018, 02:13 am Last Edit: Jun 16, 2018, 02:14 am by jremington
In C/C++
Code: [Select]
bool odd_day = day&1;


Wow, that's a really nice implementation - thanks!  I'm going to play with that in PHP, it's just elegant!


And, in PHP:
$oddDay = date('d')&1;

Thanks for the pointer, jremington.

Go Up