在月末和月初之间,不考虑具体年份

时间:2015-11-13 04:45:47

标签: php strtotime

要显示“当前季节”中可用的农产品,我可以问:

('Y-m-d',strtotime('today'))大于$start_of_season且小于$end_of_season,只要开始日期早于结束日期,逻辑就很简单。

$date_format = 'Y-m-d';
if ((date_i18n($date_format,strtotime('today')) > date_i18n($date_format,strtotime($start_of_season))) &&
     (date_i18n($date_format,strtotime('today')) < date_i18n($date_format,strtotime($end_of_season)))):

# display the produce

但如果我想忽略11月到2月之间的年份和衡量标准呢?如果今天是12月1日,它比11月更大,但也比2月更大。

我的第一次尝试就是说,

  • 如果end_date大于start_date

    • 如果今天是下半年
      • 然后拨打end_date 12-31
    • else
      • 致电start_date 01-01

这是代码:

$date_format = 'm-d';
if (date_i18n($date_format,strtotime($start_of_season)) > date_i18n($date_format,strtotime($end_of_season))):
    if (date_i18n($date_format,strtotime('today')) > date_i18n($date_format,strtotime('2000-06-01'))):
        $end_of_season = '2000-12-31';
    else:
        $start_of_season = '2000-01-01';
    endif;
endif;

if ((date_i18n($date_format,strtotime('today')) > date_i18n($date_format,strtotime($start_of_season))) &&
     (date_i18n($date_format,strtotime('today')) < date_i18n($date_format,strtotime($end_of_season)))):

对于大多数情况来说似乎“有用”。

但如果有一个项目的季节是12月到7月,而当前日期是7月1日,则会有$start_of_season > $end_of_season,但是因为它是在6月1日之后$end_of_season将被设置为12月31,把我们放在“in_season”显示窗口之外。

我确信有合理,有说服力的方法可以做到这一点,并且可以使用一些启示。

3 个答案:

答案 0 :(得分:1)

您可以构建一个允许月份的数组。然后检查月份是否在允许的月份数组中。如果是,您只需要检查您是否在本赛季的第一个月或最后一个月。如果在第一个月你必须检查一天是否大于或等于开始日。如果在上个月你必须检查日期是否小于或等于结束日。

这是一个代码示例:

function getMonthArray($startMonth, $endMonth) {
    $return = array();
    $return[] = $startMonth;

    while($startMonth != $endMonth) {
        $startMonth++;
        if ($startMonth > 12) {
            $startMonth = 1;
        }
        $return[] = $startMonth;
    }
    return $return;
}

function isInSeason($start, $end, $check) {
    $month      = (int) substr($check, 0, 2);
    $startMonth = (int) substr($start, 0, 2);
    $endMonth   = (int) substr($end, 0, 2); 
    if (in_array($month, getMonthArray($startMonth, $endMonth))) {
        $day        = (int) substr($check, 3, 2);       
        if ($month == $startMonth) {
            $startDay = (int) substr($start, 3, 2);
            return ($day >= $startDay);
        } elseif ($month == $endMonth) {
            $endDay = (int) substr($end, 3, 2);
            return ($day <= $endDay);
        } else {
            return TRUE;
        }
    }
    return FALSE;
}

$start  = '12-01';
$end    = '06-30';

$check  = '07-10';

if (isInSeason($start, $end, $check)) {
    echo 'In season';
} else {
    echo 'Not in season';
}

答案 1 :(得分:1)

你可以将你的月份标准化到赛季开始。例如,如果您的季节开始时间是11月(第11个月)并且结束于2月(第2个月),那么这些将分别标准化为第1个月和第4个月。然后你可以检查任何一个月对照标准化年份,看看它是否属于季节。

$startMonth = 11; // November
$endMonth   = 2; // February

$futureFactor = $startMonth - 1;
$pastFactor   = 13 - $startMonth;

$months = [];

// Create a normalized year to the start month.
foreach (range(1,12) as $r) {

  if ($r == $startMonth) {
    $months[$r] = 1;
  }
  else {
    $months[$r] = $r < $startMonth ? $r + $pastFactor : $r - $futureFactor;
  }
}

// Check if December(12) is in season
if ($months[12] >= $months[$startMonth] && $months[12] <= $months[$endMonth]) {
  print "In season...\n";
}
else {
  print "Out of season...\n";
}

标准化年份示例:

Array
(
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 7
    [6] => 8
    [7] => 9
    [8] => 10
    [9] => 11
    [10] => 12
    [11] => 1
    [12] => 2
)

答案 2 :(得分:1)

我个人会封装这个逻辑并使用日期对象。这是我现在想到的最简单的方法。为简洁起见,类属性是公开的。

<?php

date_default_timezone_set('America/New_York');

class Season {

    public $startMonth;
    public $startDay;
    public $endMonth;
    public $endDay;

    public function setStart($month, $day) {
        $this->startMonth = (int) $month;
        $this->startDay   = (int) $day;
    }

    public function setEnd($month, $day) {
        $this->endMonth = (int) $month;
        $this->endDay   = (int) $day;
    }

    function isActiveForDate(DateTime $date) {

        $seasonStartDate = new DateTime(sprintf('%d-%d-%d', $date->format('Y'), $this->startMonth, $this->startDay));
        $seasonEndDate   = new DateTime(sprintf('%d-%d-%d', $date->format('Y'), $this->endMonth, $this->endDay));

        // Edge case
        if ($this->startMonth > $this->endMonth) {
            $seasonStartDate->modify('-1 year');

            // This line is only useful if you wish to calculate inclusively
            if ($date == $seasonStartDate || $date == $seasonEndDate) return true;

            // If this condition is true, no need to calculate anything
            if ($date->format('n') < $this->startMonth && $date->format('n') >= $this->endMonth) {
                return false;
            }

            $seasonEndDate->modify('+1 year');
        }

        return ($date->getTimestamp() >= $seasonStartDate->getTimestamp() 
            && $date->getTimestamp() <= $seasonEndDate->getTimestamp());
    }
}

定义一个季节:

$season = new Season();
$season->setStart(11, 1);
$season->setEnd(7, 1);

测试结果:

echo ($season->isActiveForDate(new DateTime("2000-7-2")))   ? "pass" : "fail"; // fail
echo ($season->isActiveForDate(new DateTime("2000-10-31"))) ? "pass" : "fail"; // fail
echo ($season->isActiveForDate(new DateTime("2000-11-1")))  ? "pass" : "fail"; // pass
echo ($season->isActiveForDate(new DateTime("2000-6-31")))  ? "pass" : "fail"; // pass