PHP code to calculate weekend days in a certain date (Saturday, Sunday total) _php tips

Source: Internet
Author: User
Tags abs
Copy Code code as follows:

/*
| Author:yang Yu <niceses@gmail.com>
| @param char|int $start _date A valid date format, such as: 20091016,2009-10-16
| @param char|int $end _date ditto
| @return weekend days between given dates
*/
function Get_weekend_days ($start _date, $end _date) {

if (Strtotime ($start _date) > Strtotime ($end _date)) List ($start _date, $end _date) = Array ($end _date, $start _date);

$start _reduce = $end _add = 0;

$start _n = Date (' N ', Strtotime ($start _date));
$start _reduce = ($start _n = = 7)? 1:0;

$end _n = Date (' N ', Strtotime ($end _date));
In_array ($end _n,array (6,7)) && $end _add = ($end _n = 7)? 2:1;

$days = ABS (Strtotime ($end _date)-Strtotime ($start _date))/86400 + 1;

Return Floor (($days + $start _n-1-$end _n)/7 * 2-$start _reduce + $end _add;
}

Note:

Recently addressed to the company's attendance system, to automate one of these functions is to automatically write to the weekday (days of attendance) each month, and write the above function to calculate the total number of Sundays in the two-day period of Saturday, which is, of course, the simplest way to use a loop implementation, From the beginning of the day for to the end of that day, as long as the middle of Saturday or Sunday, on + +, finally easily calculated the sum, but still that sentence, the efficiency of the cycle is not good, especially when the time span is too long, miserable.

The basic idea of my function is four words: before you cut it. Don't you understand? I also feel a little puzzled ... is the number of weeks to get the start date, if less than a week, then the corresponding number of days, such as the start date is 3, then the total number of days to fill 2 days (Week 1, Week 2), if the start date is 6, then fill 5 days, that is 6-1, is the function of the $start_n- 1, if the start date is exactly Sunday, then fill up 6 days at the same time, the final results need to subtract one day (Saturday), that is, the function of the $start _reduce, OK, now "before the completion" explained. Below "cut", as the name suggests, is to cut off the number of days after the less than a week, for example, the end date is 3, then subtract 3 days from the total days, if the end date is 6 or Sunday, subtract 6 or 7, and then add 1 or 2 at the end.

There is no difficulty in the algorithm, the core idea is to adjust this time period to 7 of integers, and then multiplied by 2, in minus or plus more and less than Saturday or Sunday, is the sum of Saturday and Sunday. The final calculation of the number of days, not recommended by date (z) to calculate, because the general will not be good, related to the issue of the year, if the span of years, but also to consider the issue of a leap, it is better to calculate the direct.

Improve Records, add the $is_workday parameter, you can choose whether to return to work day, default is to return to rest day
Copy Code code as follows:

function get_weekend_days ($start _date, $end _date, $is _workday = False) {

if (strtotime ($start _date) > Strtotime ($end _date)) List ($start _date, $end _date) = Array ($end _date, $start _ Date);
$start _reduce = $end _add = 0;
$start _n = Date (' N ', Strtotime ($start _date));
$start _reduce = ($start _n = = 7)? 1:0;
$end _n = Date (' N ', Strtotime ($end _date));
In_array ($end _n,array (6,7)) && $end _add = ($end _n = 7)? 2:1;
$alldays = ABS (Strtotime ($end _date)-Strtotime ($start _date))/86400 + 1;
$weekend _days = Floor ($alldays + $start _n-1-$end _n)/7 * 2-$start _reduce + $end _add;
if ($is _workday) {
$workday _days = $alldays-$weekend _days;
return $workday _days;
}
return $weekend _days;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.