简体   繁体   English

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

[英]Check if todays date is between two other dates

I am trying to check if todays date is between START and STOP date of a period, Winter, summer, spring etc.. 我正在尝试检查今天的日期是否在某个时期的开始和停止日期之间,例如冬天,夏天,春天等。

and if the todays date is between, lets say.. the winter period, it will set the $season variable to which period it is. 并且,如果今天的日期介于冬季之间,则它将$ season变量设置为该日期。

But for the moment it just gives me "01/01", i don't understand why.. 但是目前它只给我“ 01/01”,我不明白为什么。

Thanks for help! 感谢帮助! :) :)

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

You can stick with timestamps. 您可以坚持使用时间戳。 Don't convert back to dates. 不要转换回日期。 You are making invalid comparisons such as the assumption that 30-01 is less than 28-02. 您正在进行无效的比较,例如30-01小于28-02的假设。 The computer will compare the very first 3 to the 2 and tell you that 30-01 is CORRECTLY greater than 28-02. 计算机将比较第一个3和2,然后告诉您30-01正确大于28-02。 So... 所以...

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

Now, is some date between those? 现在,两者之间有约会吗? Assume I am checking $month and $day... 假设我正在检查$ month和$ day ...

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

If you use DateTime object—which is by far the best approach—you are able to compare these with the regular comparators, eg: 如果使用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';

See documentation: http://php.net/manual/en/datetime.diff.php#example-2368 参见文档: http : //php.net/manual/en/datetime.diff.php#example-2368

Remember that a variable can be overwritten - just as the year progresses through the seasons, your variable can as well - as long as we do it in order we'll end up on the correct one. 请记住,变量可以被覆盖-正如一年中的季节变化一样,您的变量也可以被覆盖-只要我们这样做,我们就可以得出正确的变量。 This means we only have to test if our date is after the date that a season changes. 这意味着我们仅需测试日期是否季节更改的日期之后

// 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;

Here's the same logic cleaned up in a pretty function that will check any date for you and return the 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM