Php 如何通过指定的日期范围来判断季节
我将根据给定的日期指定季节。我有4个季节,按日期(不是按月份)排列。所以我决定在数组中使用Php 如何通过指定的日期范围来判断季节,php,arrays,date,range,Php,Arrays,Date,Range,我将根据给定的日期指定季节。我有4个季节,按日期(不是按月份)排列。所以我决定在数组中使用和range(),但它没有显示任何内容 这是我的密码: $p1=strtotime("2013-12-13"); $p2=strtotime("2014-02-20"); $h1a=strtotime("2014-02-21"); $h1b=strtotime("2014-04-31"); $l1=strtotime("2013-05-01"); $l2=strtotime("2013-10-31"); $
和range()
,但它没有显示任何内容
这是我的密码:
$p1=strtotime("2013-12-13");
$p2=strtotime("2014-02-20");
$h1a=strtotime("2014-02-21");
$h1b=strtotime("2014-04-31");
$l1=strtotime("2013-05-01");
$l2=strtotime("2013-10-31");
$h2a=strtotime("2013-11-01");
$h2b=strtotime("2013-12-19");
$today=strtotime(date("Y-m-d"));
if(in_array($today, range($p1, $p2))){
echo "peak";
}elseif(in_array($today, range($h1a, $h1b))){
echo "hi1";
}elseif(in_array($today, range($l1, $l2))){
echo "low";
}else(in_array($today, range($h2a, $h2b))){
echo "h2";
}
你们能改进我的代码吗
关于,,因为您的内存限制超出了范围。我只用了2天的时间,它显示了大部分的输出
可以使用大于和小于来度量日期
if($today >= $p1 && $today <= $p2){
echo "peak";
}elseif($today >= $h1a && $today <= $h1b){
echo "hi1";
}elseif($today >= $l1 && $today <= $l2){
echo "low";
}else($today >= $h2a && $today <= $h2b){
echo "h2";
}
如果($today>=$p1&&$today=$h1a&&&$today=$l1&&$today=$h2a&&$today我现在有了自己的解决方案。感谢这些试验。代码改编自:
因为您的内存限制超出了范围。我只在范围内使用了2天,它显示了大部分输出。有没有更好的方法来测试季节的当前日期?请参阅大于和小于的答案。Yogesh,您的代码看起来更好,但不起作用。它不会返回“低”如我所料。@Wilf将看到编辑过的答案。只是使用了错误顺序的比较。:)
<?
function current_season() {
// Locate the icons
$icons = array(
"peak" => "peak season",
"low" => "low season",
"high1" => "high1 season",
"high2" => "high2 season"
);
// What is today's date - number
$day = date("z");
// Days of peak
$peak_starts = date("z", strtotime("December 13"));
$peak_ends = date("z", strtotime("February 20"));
// Days of low
$low_starts = date("z", strtotime("May 1"));
$low_ends = date("z", strtotime("October 31"));
// Days of high
$high_starts = date("z", strtotime("February 21"));
$high_ends = date("z", strtotime("April 31"));
// If $day is between the days of peak, low, high, and winter
if( $day >= $peak_starts && $day <= $peak_ends ) :
$season = "peak";
elseif( $day >= $low_starts && $day <= $low_ends ) :
$season = "low";
elseif( $day >= $high1_starts && $day <= $high1_ends ) :
$season = "high";
else :
$season = "high2";
endif;
$image_path = $icons[$season];
echo $image_path;
}
echo current_season();
?>