简体   繁体   中英

PHP is date between 2 other dates - Ignoring month

I am trying to determine if a day and time are between two others, I have the following...

$currentdate = date("N h:i:s A");

This returns the day of the week as a number and then the current time in 24 hour format.

I want to check if the $currentdate is between 9am on a Friday and 9am on a Monday.

What is the best way to tackle this?

I believe this should give you what your asking for but I'm sure there are better ways to implement. So basically the time has been converted into an INT for comparing and the hours are configured not to have a leading zero hence why $timeOne and $timeTwo is shorter. I've then used an if statement to test days and time on that specific day leaving you a slot to add your code if those conditions are met.

function checkDayTime() {

$day = date(w); //0 (for Sunday) through to 6 (for Saturday)
$timeOne = 90000;
$timeTwo = 90000;//Added for easier reading
$currentTime = (int) date('Gis'); //Time as INT 00000 > 240000


 if (($day == 5 && $currentTime > $timeOne) || ($day == 6 || $day == 0) || ($day == 1 && $currentTime < $timeTwo)) {

   //Between those hours

   return TRUE;

  } else {

    //Not between those hours

   return FALSE;

  }

}

Just removed the extra if statement as it was not needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM