Testing for the first weekday of the month in PHP -


i'm working on scheduling function handle repeating events in application. 1 option 'every first weekday' (of month). have function working, i'm afraid may not using efficient method , hoping input. i'm using php 5.5. code have:

function isfirstweekday($dateobject){   $daytotest = $dateobject->format('l');   $monthtotest = $dateobject->format('f y');   $priorday = clone $dateobject;   $priorday->modify('- 1 day');   $weekdaylist = array("monday", "tuesday", "wednesday", "thursday", "friday");   //return false if it's not weekday   if (!in_array($dateobject->format('l'), $weekdaylist)) {     return false;   }   //make sure weekday first of name in month   if ($dateobject->format('y-m-d') !== date('y-m-d', strtotime("first $daytotest of $monthtotest"))) {     return false;   }   //make sure day before not weekday in same month   if (in_array($priorday->format('l'), $weekdaylist) && $priorday->format('m') === $dateobject->format('m')) {     return false;   }   return true; } 

i @ way.

1) in order first weekday, must first, second or third day of month.

2) if it's first day, can directly check if it's weekday (n = 1-5).

3) if it's second or third, in order first weekday, proceeding days must have not been week days. check if it's monday.

function isfirstweekday($dateobject) {     switch($dateobject->format('j')) {         case 1:             return $dateobject->format('n') <= 5;         case 2:         case 3:             return $dateobject->format('n') == 1;         default:             return false;     } } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -