如何确定当前 Zend_Date 时间是否在今天时间和明天时间之间?

问题描述 投票:0回答:1

您好,我正在使用

$currentDate = new Zend_Date();
来获取当前时间,但我需要确定该时间是否在晚上 10 点到凌晨 2 点之间 - 我该如何使用
Zend_Date
来做到这一点,第二天的部分是让我失望的,因为我正在比较两个不同日期的两个日期/小时之间的时间...谢谢!

php datetime zend-framework backend zend-date
1个回答
0
投票

这个问题很难正确理解。

但是,如果我理解正确,下面的代码应该可以帮助您实现您正在寻找的目标。

// 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 ...
}
© www.soinside.com 2019 - 2024. All rights reserved.