user4143585
user4143585

Reputation: 105

PHP If an Hour Is Between Two Other Hours

I need to work out if the current hour is between two other times. For example to check if the time is between 07:00 and 10:00 I could use:

$currentTime = new DateTime('09:00');
$startTime = new DateTime('07:00');
$endTime = new DateTime('10:00');

if ($currentTime->format('H:i') >= $startTime->format('H:i') && $currentTime->format('H:i') <= $endTime->format('H:i')) {
    // Do something
}

The problem I have is what happens if the time is 01:00 and I want to check if it's between 22:00 and 07:00. I'm not bothered that that it is another day, just if it falls between those two hours on a 24hr clock. Example 01:00 is between 22:00 and 07:00

22:00, 23:00, 00:00, 01:00, 02:00... 07:00

What I'm trying to achieve in the end is different prices can be set for a service between different times. So I currently loop through each hour working out what price bracket that hour falls into and the changing the price accordingly. If anyone has a more elegant solution for the problem I would be grateful to learn.

UPDATE:

Say I have a rule that says between 10pm and 7am I want to charge double. I loop through each hour from the start time to the end time and check if each hour falls between 22:00 (10pm) and 07:00 (7am) and if so it should be charged double. I want to avoid having to take in to account the date.

Upvotes: 5

Views: 22568

Answers (5)

zony
zony

Reputation: 7

$hour = (int)date('H');
echo ($hour > 7 && $hour < 22) ? 'Open' : 'Close';


$hour = (int)date('H');
echo ($hour <= 7 || $hour >= 22) ?  'Close':'Open';

Upvotes: 0

Franz Andreani
Franz Andreani

Reputation: 97

I found another way starting from the first and more popular solution that has the problem that doesn't work for me.

I have to write something between 10:00 pm and 06:00 am and the comparison does not work because when i pass midnight the endTime was always > of the start time.

so if i don't mind about the day i solved in this way:

$currentTime = new DateTime();
$startTime = new DateTime('22:00');
$endTime = (new DateTime('06:00'))->modify('+1 day');

if ($currentTime->format("a") == "am") { 
   // echo "It's a new day <br />"; 
   $currentTime = $currentTime->modify('+1 day');
}


if ($currentTime >= $startTime && $currentTime <= $endTime) {
    echo "hello";
}

Upvotes: 1

Aren Sade
Aren Sade

Reputation: 566

    //get current DateTime
    $date = new DateTime('now');
    $currentHour = 0;
    $currentMinutes = 0;
    foreach ($date as $item=>$value ) {
        if($item=="date"){
            $p = explode(" ",$value)[1];
            $hour = explode(":",$p);
            $currentHour = $hour[0];
            $currentMinutes = $hour[1];
        }
    }
    $returnVO["success"] = ((int)$currentHour<12 || ((int)$currentHour>=22 && (int)$currentMinutes > 0) ) ? false : true;

Upvotes: 0

user1811893
user1811893

Reputation: 276

<?php
$currentTime = strtotime('1:00');
$startTime = strtotime('22:00');
$endTime = strtotime('7:00');

if (
        (
        $startTime < $endTime &&
        $currentTime >= $startTime &&
        $currentTime <= $endTime
        ) ||
        (
        $startTime > $endTime && (
        $currentTime >= $startTime ||
        $currentTime <= $endTime
        )
        )
) {
    echo 'open';
} else {
    echo 'clse';
}

Upvotes: 15

John Conde
John Conde

Reputation: 219804

No need to use DateTime::format() for your comparisons. DateTime objects are already comparable.

To handle working with time periods that span midnight you will need to change the date so you have an accurate reflection of the actual date.

$currentTime = (new DateTime('01:00'))->modify('+1 day');
$startTime = new DateTime('22:00');
$endTime = (new DateTime('07:00'))->modify('+1 day');

if ($currentTime >= $startTime && $currentTime <= $endTime) {
    // Do something
}

Upvotes: 7

Related Questions