gpt4 book ai didi

php - Zend_Date : How to get the date of an upcoming day?

转载 作者:可可西里 更新时间:2023-10-31 22:49:50 25 4
gpt4 key购买 nike

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

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

我如何用 Zend_Date 做到这一点?

解决方案:

假设今天是星期二,我们想要获得即将到来的星期一。星期一是 future 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...
);

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

$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);

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

$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)

我们可以通过添加 modulus operator 来解决这个问题(百分号)添加到我们的公式中以获得除法运算的余数。

$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;
}

最佳答案

逻辑如下:

$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

关于php - Zend_Date : How to get the date of an upcoming day?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1931349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com