Calculate the php code for the number of weekend days in a period of time (Saturday, Sunday total)

Source: Internet
Author: User

Copy codeThe Code is as follows :/*
| Author: Yang Yu <niceses@gmail.com>
| @ Param char | int $ start_date is a valid date format, for example, 20091016,2009-10-16.
| @ Param char | int $ end_date same as above
| @ Return the number of weekend days between a given date
*/
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:

One of the functions of the attendance system recently written to the company is to automatically write the work days (attendance days) of each month into the system. Therefore, the above functions are written, it is used to calculate the total number of Saturday and Sunday in two dates. This function is of course the easiest to implement in a loop. From the day of the start to the day of the end, in the middle, as long as it is Saturday or Sunday, ++ is used to calculate the sum easily. However, the cycle efficiency is really bad, especially when the time span is too long.

The basic idea of this function is to subscribe to and subscribe to it. Don't you understand? I also think it's a bit confusing... Is to get the number of weeks of the start date. If less than one week is used, fill in the corresponding number of days. For example, if the start date is 3 days of the week, then fill in the total number of days (1 day of the week, 2 days of the week ), if the start date is 6 days of the week, add 5 days, that is, 6-1, that is, $ start_N-1 in the function. If the start date happens to be Sunday, add 6 days at the same time, the final result needs to be subtracted from the day (Saturday), that is, $ start_reduce in the function. Now the Explanation of "Add before" is complete. Next we will talk about "post-cut". As the name suggests, we will cut off the remaining days in less than a week. For example, if the end date is 3 in a week, we will subtract 3 days from the total number of days, if the end date is 6 or Sunday of the week, 1 or 2 will be added at the end of the week after 6 or 7 is subtracted.

There is no difficulty in the algorithm. The core idea is to adjust the time period to an integer of 7 and multiply it by 2 to subtract or add the plus and minus Calculation on Saturday or Sunday, the sum of Saturday and Sunday is obtained. We do not recommend that you use date (z) to calculate the number of days in a certain period of time. Because of the poor universality, the cross-year problem is involved. If the problem persists for many years, you must consider the leap year problem, it is better to calculate it directly.

Improvement recordTo add the $ is_workday parameter. You can choose whether to return the work day. The default value is to return the rest day.Copy codeThe Code is 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.