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!
Asked
Active
Viewed 21 times
0

Pippy Longstocking
- 261
- 5
- 12
-
For a start, you need to convert dates to a format that allows direct comparisons (e.g. integers in the 0 to 23 range) and then determine whether start is less or equal than end (same day) or not (different days). – Álvaro González Aug 05 '23 at 12:25
1 Answers
1
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 ...
}

Maurizio
- 469
- 1
- 4
- 11
-
thank you for the response - however i have a question: this will not work if the current date is between 12am and 2am right? because it will check 2am upper bound for the NEXT day and not now? or am I mistaken? thank you! – Pippy Longstocking Aug 05 '23 at 11:33