Zend_Date:如何获取即将到来的一天的日期?

发布于 2024-08-15 07:40:58 字数 2922 浏览 5 评论 0原文

我想获取未来最近的星期一的日期(即不是过去)。

因此,如果今天是星期二(2009 年 12 月 1 日),我想获取星期一(2009 年 12 月 7 日)的日期。

我如何使用 Zend_Date 做到这一点?

解决方案:

假设今天是星期二,我们想要获得即将到来的星期一。星期一是 6 天后的未来。因此,我们将添加 6 天来获取星期一的日期。

//like so:
$tuesday = Zend_Date::now();
$nextMonday = $tuesday->addDay(6);

要动态地执行此操作,我们需要确定今天是一周中的哪一天:

$today = Zend_Date::now();
$dayNumber = $today->get(Zend_Date::WEEKDAY_DIGIT);
//dayNumber will now be equal to the numeric day of the week (0-6)
//example:
$weekdays = array(
    0 => 'sunday',
    1 => 'monday',
    2 => 'tuesday' //etc...
);

要确定需要添加多少天才能获得所需的未来日期,我们执行以下操作:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 );
#      $dayWanted = monday(1)
# $todayDayNumber = tuesday(2)
#               7 = number of days in a week (we don't want a negative number)
#       1 - 2 + 7 = 6 days into the future
$nextMonday = $today->addDay($daysToAdd);

假设我们想要的那一天是星期三(明天) ),未来的某一天。我们之前的解决方案不起作用:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 );
#      $dayWanted = wednesday(3)
# $todayDayNumber = tuesday(2)
#               7 = number of days in a week
#       3 - 2 + 7 = 8 days into the future (not 1)

我们可以通过添加 来解决此问题将模运算符(百分号)添加到我们的公式中即可获得除法运算的余数。

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 ) % 7;
# (3 - 2 + 7) % 7
# $daysToAdd == 1 (remainder of 8 divided by 7)
$tomorrow = $today->addDay($daysToAdd);

现在我们的公式将按预期工作......除了一件事之外。如果今天是星期二,并且我想得到下星期二,我们的公式将在今天返回,而不是从今天起一周:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 ) % 7;
# (2 - 2 + 7) % 7 == 0
# 7 goes into 7 evenly with no remainder

我们将必须添加一个检查以确保它不等于零。

if ($daysToAdd == 0) {
    //give me the date a week from today, not today's date
    $daysToAdd = 7;
}

最终解决方案:

public function outputDate()
{
    $monday = $this->getDateOfNext('monday');
    echo 'today: ' . Zend_Date::now()->toString(Zend_Date::RFC_850) . "<br>";
    echo "monday: " . $monday->toString(Zend_Date::RFC_850);
}

private function getDateOfNext($dayWanted)
{
    $weekdays = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
    if (!in_array($dayWanted, $weekdays)) {
        throw new Exception("'$dayWanted' not found in array of possible weekdays");
    }
    $weekdays = array_flip($weekdays);
    $date = Zend_Date::now();
    $today = $date->get(Zend_Date::WEEKDAY_DIGIT);
    $daysToAdd = ( $weekdays[$dayWanted] - $today + 7 ) % 7;
    if ($daysToAdd == 0) {
        //give me the date a week from today, not today's date
        $daysToAdd = 7;
    }
    $date->addDay($daysToAdd);
    return $date;
}

I want to get the date of the closest Monday in the future (i.e. not in the past).

So if today is Tuesday (Dec 1, 2009) I want to get the date of Monday (Dec 7, 2009).

How can I do this with Zend_Date?

Solution:

Let's say today is Tuesday and we wanted to get the upcoming Monday. Monday is 6 days into the future. So, we would add 6 days to get monday's date.

//like so:
$tuesday = Zend_Date::now();
$nextMonday = $tuesday->addDay(6);

To do this dynamically, we will need to determine which day of the week it is today:

$today = Zend_Date::now();
$dayNumber = $today->get(Zend_Date::WEEKDAY_DIGIT);
//dayNumber will now be equal to the numeric day of the week (0-6)
//example:
$weekdays = array(
    0 => 'sunday',
    1 => 'monday',
    2 => 'tuesday' //etc...
);

To determine how many days we need to add to get the desired future day, we do the following:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 );
#      $dayWanted = monday(1)
# $todayDayNumber = tuesday(2)
#               7 = number of days in a week (we don't want a negative number)
#       1 - 2 + 7 = 6 days into the future
$nextMonday = $today->addDay($daysToAdd);

Let's say the day we want is wednesday (tomorrow), one day into the future. Our previous solution won't work:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 );
#      $dayWanted = wednesday(3)
# $todayDayNumber = tuesday(2)
#               7 = number of days in a week
#       3 - 2 + 7 = 8 days into the future (not 1)

We can solve this problem by adding the modulus operator (percent sign) to our formula to get the remainder of a division operation.

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 ) % 7;
# (3 - 2 + 7) % 7
# $daysToAdd == 1 (remainder of 8 divided by 7)
$tomorrow = $today->addDay($daysToAdd);

Now our formula will work as expected...with the exception of one thing. If today is tuesday, and I want to get next tuesday, our formula will return today instead of a week from today:

$daysToAdd = ( $dayWanted - $todayDayNumber + 7 ) % 7;
# (2 - 2 + 7) % 7 == 0
# 7 goes into 7 evenly with no remainder

We will have to add a check to make sure it is not equal to zero.

if ($daysToAdd == 0) {
    //give me the date a week from today, not today's date
    $daysToAdd = 7;
}

Final Solution:

public function outputDate()
{
    $monday = $this->getDateOfNext('monday');
    echo 'today: ' . Zend_Date::now()->toString(Zend_Date::RFC_850) . "<br>";
    echo "monday: " . $monday->toString(Zend_Date::RFC_850);
}

private function getDateOfNext($dayWanted)
{
    $weekdays = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
    if (!in_array($dayWanted, $weekdays)) {
        throw new Exception("'$dayWanted' not found in array of possible weekdays");
    }
    $weekdays = array_flip($weekdays);
    $date = Zend_Date::now();
    $today = $date->get(Zend_Date::WEEKDAY_DIGIT);
    $daysToAdd = ( $weekdays[$dayWanted] - $today + 7 ) % 7;
    if ($daysToAdd == 0) {
        //give me the date a week from today, not today's date
        $daysToAdd = 7;
    }
    $date->addDay($daysToAdd);
    return $date;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-08-22 07:40:58

逻辑如下:

$days_per_week = 7;
$weekdays = array_flip(array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'));

$day_wanted = 'mon';
$days_forward =
  ( $weekdays[$day_wanted] - $date->get(Zend_Date::WEEKDAY_DIGIT) + $days_per_week )
  % $days_per_week;

$date->addDay($days_forward);

这对于任何 $day_wanted 都很有效。

Here's the logic, laid out:

$days_per_week = 7;
$weekdays = array_flip(array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'));

$day_wanted = 'mon';
$days_forward =
  ( $weekdays[$day_wanted] - $date->get(Zend_Date::WEEKDAY_DIGIT) + $days_per_week )
  % $days_per_week;

$date->addDay($days_forward);

That works nicely for any $day_wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文