包括基于日期范围的文件

时间:2014-01-08 20:24:31

标签: php include

我正在尝试根据日期范围显示商店营业时间。我需要显示正常时间,除了在6月15日到9月1日之间使用夏令时。我目前在我的代码(下面)中都有两个,我只是评论一行。有人可以帮我自动化吗?

<?php
//include ("content/summer_hours.inc");
include ("content/normal_hours.inc");
?>

2 个答案:

答案 0 :(得分:0)

关掉众所周知的袖口......

$june15=strtotime("15 June ".date('Y'));
$sep1=strtotime("1 September ".date('Y'));
$now=time();
if ($now > $june15 && $now < $sep1){
  include ("content/summer_hours.inc");
}
else{
  include ("content/normal_hours.inc");
}

答案 1 :(得分:0)

这是另一种方法,用于比较一年中的某一天,并查看它是否介于您要查找的一年中的某一天之间。可能会更有效率。这可能是一种更快的方法。

$year = date('Y'); //Get the current year
$today = date('z'); //Get the day of the year.
$june15 = 166; //166th day of year
$sept01 = 244; //244th day of year
$isleap = (($year & 3) == 0 && (($year % 25) != 0 || ($year & 15) == 0)); //Is it a leap year.

if ($today > $june15 + $isleap && $today < $sept01 + $isleap) {
    include ("content/summer_hours.inc");
} else {
    include ("content/normal_hours.inc");
}