brooksh Posted November 6, 2008 Share Posted November 6, 2008 I'm trying to write a script that will find a day by counting weekdays, but skipping the weekends. Does anyone have any ideas how to accomplish this? For example: $delivery_date=mktime($hour, $minute, $second, $month, $day+$number_of_days, $year); $number_of_days is always different. if $number_of_days = "4" And today is Thursday, the result day should be Wednesday. Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/ Share on other sites More sharing options...
corbin Posted November 6, 2008 Share Posted November 6, 2008 This will work PHP5.1+ $today = date("N"); $number_of_days = 4; //example value //this example will assume today is not a weekend day, because I don't know how you want to handle that. $delivery = ((int) $today) + $number_of_days; if($delivery > 5) { //Saturday or Sunday $weeks = floor($delivery/5); $days = $delivery % 5; //so, for example, if delivery were 8, we would have 1 weeks and 3 days. $delivery = (($weeks-1)*7) + $days; } $delivery_date = date("l jS \of F Y", time()+($delivery*24*60*60)); My offsets might be off, so you'll probably want to test that. ;p Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684147 Share on other sites More sharing options...
brooksh Posted November 6, 2008 Author Share Posted November 6, 2008 I found this script, but it tells me how many work days there are between two dates. ?php /** * Calculates the number of work days between 2 given times * * @see get_holidays() * * @param date $start_date First date * @param date $end_date Second date * @param bool $workdays_only Whether to count only work days (eg. Mon-Fri) * @param bool $skip_holidays Whether to use the get_holidays() function to skip holiday days as well * @return int $workday_counter Number of workdays between the 2 dates */ function date_difference($start_date, $end_date, $workdays_only = false, $skip_holidays = false){ $start_date = strtotime($start_date); $end_date = strtotime($end_date); $seconds_in_a_day = 86400; $sunday_val = "0"; $saturday_val = "6"; $workday_counter = 0; $holiday_array = array(); $ptr_year = intval(date("Y", $start_date)); $holiday_array[$ptr_year] = get_holidays(date("Y", $start_date)); for($day_val = $start_date; $day_val <= $end_date; $day_val+=$seconds_in_a_day){ $pointer_day = date("w", $day_val); if($workdays_only == true){ if(($pointer_day != $sunday_val) AND ($pointer_day != $saturday_val)){ if($skip_holidays == true){ if(intval(date("Y", $day_val))!=$ptr_year){ $ptr_year = intval(date("Y", $day_val)); $holiday_array[$ptr_year] = get_holidays(date("Y", $day_val)); } if(!in_array($day_val, $holiday_array[date("Y", $day_val)])){ $workday_counter++; } }else{ $workday_counter++; } } }else{ if($skip_holidays == true){ if(intval(date("Y", $day_val))!=$ptr_year){ $ptr_year = intval(date("Y", $day_val)); $holiday_array[$ptr_year] = get_holidays(date("Y", $day_val)); } if(!in_array($day_val, $holiday_array[date("Y", $day_val)])){ $workday_counter++; } }else{ $workday_counter++; } } } return $workday_counter; } /** * Takes a date in yyyy-mm-dd format and returns a PHP timestamp * * @param string $MySqlDate * @return unknown */ function get_timestamp($MySqlDate){ $date_array = explode("-",$MySqlDate); // split the array $var_year = $date_array[0]; $var_month = $date_array[1]; $var_day = $date_array[2]; $var_timestamp = mktime(0,0,0,$var_month,$var_day,$var_year); return($var_timestamp); // return it to the user } /** * Returns the date of the $ord $day of the $month. * For example ordinal_day(3, 'Sun', 5, 2001) returns the * date of the 3rd Sunday of May (ie. Mother's Day). * * @author [email protected] * * @param int $ord * @param string $day (must be 3 char abbrev, per date("D) * @param int $month * @param int $year * @return unknown */ function ordinal_day($ord, $day, $month, $year) { $firstOfMonth = get_timestamp("$year-$month-01"); $lastOfMonth = $firstOfMonth + date("t", $firstOfMonth) * 86400; $dayOccurs = 0; for ($i = $firstOfMonth; $i < $lastOfMonth ; $i += 86400){ if (date("D", $i) == $day){ $dayOccurs++; if ($dayOccurs == $ord){ $ordDay = $i; } } } return $ordDay; } function memorial_day($inc_year){ for($date_stepper = intval(date("t", strtotime("$inc_year-05-01"))); $date_stepper >= 1; $date_stepper--){ if(date("l", strtotime("$inc_year-05-$date_stepper"))=="Monday"){ return strtotime("$inc_year-05-$date_stepper"); break; } } } /** * Looks through a lists of defined holidays and tells you which * one is coming up next. * * @author [email protected] * * @param int $inc_year The year we are looking for holidays in * @return array */ function get_holidays($inc_year){ //$year = date("Y"); $year = $inc_year; $holidays[] = new Holiday("New Year's Day", get_timestamp("$year-1-1")); $holidays[] = new Holiday("Australia Day", get_timestamp("$year-1-26")); $holidays[] = new Holiday("Labour Day", ordinal_day(1, 'Mon', 3, $year)); $holidays[] = new Holiday("Anzac Day", get_timestamp("$year-4-25")); //$holidays[] = new Holiday("St. Patrick's Day", get_timestamp("$year-3-17")); // TODO: $holidays[] = new Holiday("Good Friday", easter_date($year)); $holidays[] = new Holiday("Easter", easter_date($year)); // TODO: $holidays[] = new Holiday("Easter Monday", easter_date($year)); $holidays[] = new Holiday("Foundation Day", ordinal_day(1, 'Mon', 6, $year)); $holidays[] = new Holiday("Queen's Birthday", ordinal_day(1, 'Mon', 10, $year)); //$holidays[] = new Holiday("Memorial Day", memorial_day($year)); //$holidays[] = new Holiday("Mother's Day", ordinal_day(2, 'Sun', 5, $year)); //$holidays[] = new Holiday("Father's Day", ordinal_day(3, 'Sun', 6, $year)); //$holidays[] = new Holiday("Independence Day", get_timestamp("$year-7-4")); //$holidays[] = new Holiday("Labor Day", ordinal_day(1, 'Mon', 9, $year)); $holidays[] = new Holiday("Christmas", get_timestamp("$year-12-25")); $holidays[] = new Holiday("Boxing Day", get_timestamp("$year-12-26")); $numHolidays = count($holidays) - 1; $out_array = array(); for ($i = 0; $i < $numHolidays; $i++){ $out_array[] = $holidays[$i]->date; } unset($holidays); return $out_array; } class Holiday{ //var $name; //var $date; public $name; public $date; // Contructor to define the details of each holiday as it is created. function holiday($name, $date){ $this->name = $name; // Official name of holiday $this->date = $date; // UNIX timestamp of date } } ?> Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684162 Share on other sites More sharing options...
harristweed Posted November 6, 2008 Share Posted November 6, 2008 I think Corbins answer is very good however if you don't have php 5.1 I'd do this.... /* 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday */ $today=date("w"); if(($today==1 && $number_of_days < 5)|| ($today==2 && $number_of_days < 4)||($today==3 && $number_of_days < 3)||($today==4 && $number_of_days < 2)){ $delivery_date=mktime($hour, $minute, $second, $month, $day+$number_of_days, $year); }else{ //add 2days for every weekend $weekends=floor($number_of_days/7)*2; $total_days=$number_of_days+$weekends; $delivery_date=mktime($hour, $minute, $second, $month, $day+$total_days, $year); } Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684186 Share on other sites More sharing options...
brooksh Posted November 7, 2008 Author Share Posted November 7, 2008 I appreciate your help, but the script doesn't work. <?php function find_day($transit_time){ /* 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday */ $today = time (); $year = date("Y", $today); $month = date("m", $today); $day = date("j", $today); $today_day = date("l", $today); $hour= date("H", $today); $weekday = date("w"); if(($weekday==1 && $transit_time < 5)|| ($weekday==2 && $transit_time < 4)||($weekday==3 && $transit_time < 3)||($weekday==4 && $transit_time < 2)){ $delivery_date=mktime($hour, $minute, $second, $month, $day+$transit_time, $year); }else{ //add 2days for every weekend $weekends=floor($transit_time/7)*2; $total_days=$transit_time+$weekends; $delivery_date=mktime($hour, $minute, $second, $month, $day+$total_days, $year); } return $delivery_date; }//end function find_day $transit_time = "3"; $delivery_date = find_day($transit_time); echo date("l F j, Y", $delivery_date); ?> Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684215 Share on other sites More sharing options...
harristweed Posted November 7, 2008 Share Posted November 7, 2008 Sorry, It was a bit late last nigt. Try: <?php function find_day($transit_time){ $today = time (); $year = date("Y", $today); $month = date("m", $today); $day = date("j", $today); $today_day = date("l", $today); $hour= date("H", $today); $weekday = date("w"); if(($weekday==1 && $transit_time < 5)|| ($weekday==2 && $transit_time < 4)||($weekday==3 && $transit_time < 3)||($weekday==4 && $transit_time < 2)){ $delivery_date=mktime($hour, $minute, $second, $month, $day+$transit_time, $year); }else{ //add 2days for every weekend $weekends=floor($transit_time/7)*2; //already one weekend because we got to this bit! This calculates additional weekends $total_days=$transit_time+$weekends+2;// need to add one that weekend! $delivery_date=mktime($hour, $minute, $second, $month, $day+$total_days, $year); } return $delivery_date; }//end function find_day $transit_time = "3"; $delivery_date = find_day($transit_time); echo date("l F j, Y", $delivery_date); ?> Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684401 Share on other sites More sharing options...
brooksh Posted November 7, 2008 Author Share Posted November 7, 2008 Thanks. That works. Link to comment https://forums.phpfreaks.com/topic/131705-solved-how-to-skip-weekends-script/#findComment-684673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.