[英]Check if todays date is between two other dates
我正在嘗試檢查今天的日期是否在某個時期的開始和停止日期之間,例如冬天,夏天,春天等。
並且,如果今天的日期介於冬季之間,則它將$ 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";
}
您可以堅持使用時間戳。 不要轉換回日期。 您正在進行無效的比較,例如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";
如果使用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
請記住,變量可以被覆蓋-正如一年中的季節變化一樣,您的變量也可以被覆蓋-只要我們這樣做,我們就可以得出正確的變量。 這意味着我們僅需測試日期是否在季節更改的日期之后 。
// 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.