How do I find out if current Zend_Date time is between time today and time tomorrow?

38 Views Asked by At

Hello I am using $currentDate = new Zend_Date(); to get the current time but I need to determine if that time is between say 10pm and 2am - how can I do this with Zend_Date, the next day part is what is throwing me off since I'm comparing a time between two dates/hours in two different days...thank you!

1

There are 1 best solutions below

1
On

The question was difficult to understand properly.

However, If I understood it right, the code below should help you achieve what you're looking for.

// create a variable defining the current datetime
$currentDate = new Zend_Date();

// set the lower bound for 10 PM
$lowerBound = new Zend_Date();
$lowerBound->setHour(22);
$lowerBound->setMinute(0);
$lowerBound->setSecond(0);

// set the upper bound for 02 AM (02:00, the next day)
$upperBound = new Zend_Date();
$upperBound->addDay(1); // here we're forcing to add 1 day
$upperBound->setHour(2);
$upperBound->setMinute(0);
$upperBound->setSecond(0);

// condition
if ($currentDate->isLater($lowerBound) && $currentDate->isEarlier($upperBound)) {
    ... do your stuff ...
}