【发布时间】:2014-02-05 12:41:51
【问题描述】:
旺季: 4 月 28 日 - 9 月 30 日, 12 月 27 日 - 1 月 3 日
淡季: 10 月 1 日 - 12 月 26 日, 1 月 4 日 - 4 月 27 日
我给了一个日期来检查:2014-02-18,我想在哪个季节包括它的情况下有 TRUE 或 FALSE。不分今年怎么办?
【问题讨论】:
旺季: 4 月 28 日 - 9 月 30 日, 12 月 27 日 - 1 月 3 日
淡季: 10 月 1 日 - 12 月 26 日, 1 月 4 日 - 4 月 27 日
我给了一个日期来检查:2014-02-18,我想在哪个季节包括它的情况下有 TRUE 或 FALSE。不分今年怎么办?
【问题讨论】:
尝试简单的日期比较:
function is_high_season($date) {
$md = gmdate('m-d', strtotime($date));
return
('03-28' <= $md && $md <= '09-30') ||
('12-27' <= $md && $md <= '12-31') ||
('01-01' <= $md && $md <= '01-03');
}
【讨论】:
if 语句的解决方案?此解决方案不适合您吗?
$day = $d->format('j');
$month = $d->format('n');
if($month == 1 && $day <= 3) {
return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
return 'low';
} elseif($month == 4 && $day >= 28) {
return 'high';
} elseif($month < 10) {
return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
return 'low';
} elseif($month == 12 && $day >= 27) {
return 'high';
}
【讨论】:
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
//echo $Date; // echos today!
$hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
$hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
$hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
$hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));
$lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
$lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
$lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
$lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));
if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
{
echo "is Hi Season";
}
if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
{
echo "is Hi Season";
}
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
{
echo "is Low Season";
}
if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
{
echo "is Low Season";
}
else
{
echo "Date doesn't fall in a season!";
}
【讨论】: