Home Backend Development PHP Tutorial 计算一段日期内的周末天数的php代码(星期六,星期日总和)_php技巧

计算一段日期内的周末天数的php代码(星期六,星期日总和)_php技巧

May 17, 2016 am 09:29 AM

复制代码 代码如下:

/*
| Author: Yang Yu
| @param char|int $start_date 一个有效的日期格式,例如:20091016,2009-10-16
| @param char|int $end_date 同上
| @return 给定日期之间的周末天数
*/
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;
}

备注:

最近写给公司用的考勤系统,把其中的一个功能自动化,就是每个月的工作日(出勤天数)改为自动写入,于是写出以上函数,用来计算两个日期内的周六周日总数,稍微解释下吧,这个功能当然是用循环实现是最简单的,从开始那天for到结束那天,中间只要是周六或周日,就++,最后轻易算出总和,但还是那句话,循环的效率实在是不好,尤其当时间跨度过长时,惨不忍睹。

我这个函数的基本思路是四个字:前补后砍。没听懂吧?我也觉得有点莫名其妙。。。就是取得开始日期的星期数,如果不足一周,则补上对应的天数,比如开始日期是星期3,那么总天数就补上2天(星期1,星期2),如果开始日期是星期6,则补上5天,也就是6-1,就是函数中的$start_N - 1,如果开始日期恰好是周日,那么补上6天的同时,最后的结果需要减去一天(周六),也就是函数中的 $start_reduce ,好了,现在“前补”解释完了。下面讲下“后砍”,顾名思义,就是将后面多余的不足一周的天数,砍掉,例如,结束日期为星期3,那么就从总天数里减去3天,如果结束日期为星期6或者星期天,那么减去6或7的同时,还要在最后补上1或2。

算法没什么难点,核心思想就是将这个时间段调整为7的整数,然后乘以2,在减去或加上多算和少算的周六或周日,得到的就是星期六和星期日的总和。最后算一段时间内的天数,不建议用date(z)来算,因为通用性会不好,涉及到跨年的问题,如果跨多年,还要考虑闰年的问题,倒不如这样算来的直接。

改进记录,加入$is_workday 参数,可以选择是否返回工作日,默认是返回休息日
复制代码 代码如下:

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;
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1247
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles