检查今天的日期是否在其他两个日期之间

时间:2014-11-18 17:02:34

标签: php

我试图检查今天的日期是否在一段时间的开始和停止日期之间,冬季,夏季,春季等。

如果今天的日期介于两者之间,假设...冬季期间,它会将$ season变量设置为它所处的时间段。

但目前它只是给了我" 01/01",我不明白为什么......

感谢您的帮助! :)

$season = date("d-m");
$season = date("d-m", strtotime($season));


$startSummer = date("01-06");
$endSummer = date("31-08");

$startAutum = date("01-09");
$endAutum = date("30-11");

$startSpring = date("01-03");
$endSpring = date("31-05");

$startWinter = date("01-12");
$endWinter = date("28-02");

// start and stop, periods

// $startYear = date("d-m", strtotime($startYear));         $endYear = date("d-m", strtotime($endYear));
$startSummer = date("d-m", strtotime($startSummer));      $endSummer = date("d-m", strtotime($endSummer));
$startAutum = date("d-m", strtotime($startAutum));        $endAutum = date("d-m", strtotime($endAutum));
$startSpring = date("d-m", strtotime($startSpring));      $endSpring = date("d-m", strtotime($endSpring));
$startWinter = date("d-m", strtotime($startWinter));      $endWinter = date("d-m", strtotime($endWinter));

  if(($season > $startSummer) && ($season < $endSummer)){
    $season = "Sommar";
  }else if(($season > $startAutum) && ($season < $endAutum)){
    $season = "Höst";
  }else if(($season > $startSpring) && ($season < $endSpring)){
    $season = "Vår";
  }else if(($season > $startWinter) && ($season < $endWinter)){
    $season = "Vinter";
  }

3 个答案:

答案 0 :(得分:1)

你可以坚持使用时间戳。不要转换回日期。您正在进行无效比较,例如假设30-01小于28-02。计算机将比较前3到2,并告诉您30-01正确地大于28-02。所以......

$startSummer = mktime(0,0,0, 6, 1, 2000); // The year doesn't matter according to your code
$endSummer = mktime(0,0,0, 8, 31, 2000);

现在,这些之间是否有些约会?假设我正在检查$ month和$ day ......

$myday = mktime(0,0,0, $month, $day, 2000);
if($myday>=$startSummer && $myday<=$endSummer) $season = "Summer";

答案 1 :(得分:1)

如果使用DateTime对象 - 这是迄今为止最好的方法 - 您可以将它们与常规比较器进行比较,例如:

$date1 = new DateTime('today');
$date2 = new DateTime('2014-04-04');

if ($date1 < $date2) echo 'Past';
else if ($date1 == $date2) echo 'Present';
else echo 'Future';

请参阅文档:http://php.net/manual/en/datetime.diff.php#example-2368

答案 2 :(得分:0)

请记住,变量可以被覆盖 - 正如年份在整个季节中一样,你的变量也可以 - 只要我们这样做,我们就会得到正确的变量。这意味着我们只需要测试我们的日期是之后一个季节变化的日期。

// Since we're testing today's date
// we use the current year timestamps
$year = date('Y');
$startSpring = strtotime("$year-03-01");
$startSummer = strtotime("$year-06-01");
$startAutum = strtotime("$year-09-01");
$startWinter = strtotime("$year-12-01");

$today = time();

// The year starts with Winter
$season = 'Winter';
if($today > $startSpring) $season = 'Spring'; // Past the 1st day of spring?
if($today > $startSummer) $season = 'Summer'; // Etc...
if($today > $startAutumn) $season = 'Autumn';
if($today > $startWinter) $season = 'Winter';

echo 'It is currently '.$season;

这里有一个相同的逻辑清理了一个漂亮的功能,它将为你检查任何日期并返回赛季:

// Accepts an optional unix timestamp
// Uses the current date by default
function getSeason($test_date=FALSE){
    $test_date = $test_date ? $test_date : time();

    // Use the year of the date we're testing
    $year = date('Y', $test_date);

    // The year starts with Winter
    $season = 'Winter';
    if($test_date > strtotime("$year-03-01")) $season = 'Spring'; // Past the 1st day of spring?
    if($test_date > strtotime("$year-06-01")) $season = 'Summer'; // Etc...
    if($test_date > strtotime("$year-09-01")) $season = 'Autumn';
    if($test_date > strtotime("$year-12-01")) $season = 'Winter';

    return $season;
}
相关问题