我正在试着检查今天的日期是否介于一段时期的开始日期和停止日期之间,冬天,夏天,春天等等。
如果今天的日期介于..。在冬季,它会将$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";
}
发布于 2014-11-18 09:28:07
记住,一个变量可以被覆盖--就像一年四季的进展一样,你的变量也可以--只要我们这样做,我们就会得到正确的变量。这意味着我们只需要测试我们的日期是否是一个季节变化的日期之后。
// 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;
}
发布于 2014-11-18 09:08:24
你可以坚持使用时间戳。不要改回日期。您正在进行无效的比较,例如假设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";
发布于 2014-11-18 09:17:08
如果您使用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';
https://stackoverflow.com/questions/26999831
复制