PHP programming to calculate whether there is an intersection between two time periods (not counted as overlapping boundary), intersection Boundary

Source: Internet
Author: User

PHP programming to calculate whether there is an intersection between two time periods (not counted as overlapping boundary), intersection Boundary

This article describes how to calculate the intersection of two time periods in PHP programming. We will share this with you for your reference. The details are as follows:

Versions before optimization:

/*** PHP calculates whether there is an intersection between two time periods (Boundary overlap is not counted) ** @ param string $ beginTime1 start time 1 * @ param string $ endTime1 end time 1 * @ param string $ beginTime2 start time 2 * @ param string $ endTime2 end time 2 * @ return bool */function is_time_cross ($ beginTime1 = '', $ endTime1 = '', $ beginTime2 ='', $ endTime2 = '') {$ status = $ beginTime2-$ beginTime1; if ($ status> 0) {$ status2 = $ beginTime2-$ endTime1; if ($ status2> 0) {return false;} elseif ($ status2 <0) {return true ;} else {return false ;}} elseif ($ status <0) {$ status2 = $ endTime2-$ beginTime1; if ($ status2> 0) {return true ;} else if ($ status2 <0) {return false;} else {$ status2 = $ endTime2-$ beginTime1; if ($ status2 = 0) {return false;} else {return true ;}}}

Optimized version (condition merge ):

/*** PHP calculates whether there is an intersection between two time periods (Boundary overlap is not counted) ** @ param string $ beginTime1 start time 1 * @ param string $ endTime1 end time 1 * @ param string $ beginTime2 start time 2 * @ param string $ endTime2 end time 2 * @ return bool */function is_time_cross ($ beginTime1 = '', $ endTime1 = '', $ beginTime2 ='', $ endTime2 = '') {$ status = $ beginTime2-$ beginTime1; if ($ status> 0) {$ status2 = $ beginTime2-$ endTime1; if ($ status2 >=0) {return false;} else {return true ;}} else {$ status2 = $ endTime2-$ beginTime1; if ($ status2> 0) {return true;} else {return false ;}}}

Test:

$ BeginTime1 = strtotime ('2017-08-07 '); $ endTime1 = strtotime ('2017-08-07 '); $ beginTime2 = strtotime ('2017-08-07 '); $ endTime2 = strtotime ('2017-08-07'); echo is_time_cross ($ beginTime1, $ endTime1, $ beginTime2, $ endTime2); // output 1

PS: here we recommend several time and date related tools for your reference:

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online date calculator/days calculator:
Http://tools.jb51.net/jisuanqi/datecalc

Online date-days difference calculator:
Http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime

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.