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