Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查今天的日期是否介于其他两个日期之间_Php - Fatal编程技术网

Php 检查今天的日期是否介于其他两个日期之间

Php 检查今天的日期是否介于其他两个日期之间,php,Php,我试图检查今天的日期是否介于某个时段(冬季、夏季、春季等)的开始日期和结束日期之间 如果今天的日期介于…和…之间。。在冬季期间,它将设置$SECTION变量,并将其指定为哪个期间 但是现在它只给了我“01/01”,我不明白为什么 谢谢你的帮助!:) $seasure=日期(“d-m”); $season=日期(“d-m”,标准时间($season)); $startSummer=日期(“01-06”); $endSummer=日期(“31-08”); $startAutum=日期(“01-09”

我试图检查今天的日期是否介于某个时段(冬季、夏季、春季等)的开始日期和结束日期之间

如果今天的日期介于…和…之间。。在冬季期间,它将设置$SECTION变量,并将其指定为哪个期间

但是现在它只给了我“01/01”,我不明白为什么

谢谢你的帮助!:)

$seasure=日期(“d-m”);
$season=日期(“d-m”,标准时间($season));
$startSummer=日期(“01-06”);
$endSummer=日期(“31-08”);
$startAutum=日期(“01-09”);
$endAutum=日期(“30-11”);
$startSpring=日期(“01-03”);
$endSpring=日期(“31-05”);
$startwitter=日期(“01-12”);
$endWinter=日期(“28-02”);
//开始和停止,句号
//$startYear=日期(“d-m”,标准时间($startYear))$endYear=日期(“d-m”,标准时间($endYear));
$startSummer=日期(“d-m”,标准时间($startSummer))$夏末=日期(“d-m”,标准时间($夏末));
$startAutum=日期(“d-m”,标准时间($startAutum))$endAutum=日期(“d-m”,标准时间($endAutum));
$startSpring=日期(“d-m”,strottime($startSpring))$endSpring=日期(“d-m”,标准时间($endSpring));
$startWinter=日期(“d-m”,标准时间($startWinter))$endWinter=日期(“d-m”,标准时间($endWinter));
如果(($seasure>$startsumer)和($seasure<$endSummer)){
$season=“Sommar”;
}否则如果($SEASH>$startAutum)和($SEASH<$endAutum)){
$season=“Höst”;
}else if(($season>$startSpring)和($season<$endSpring)){
$season=“Vår”;
}否则如果($SECTION>$startWinter)和($SECTION<$endWinter)){
$seasure=“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";
$myday=mktime(0,0,0,$month,$day,2000);

如果($myday>=$startSummer&&$myday如果您使用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';
$date1=新日期时间(“今天”);
$date2=新日期时间('2014-04-04');
如果($date1<$date2)回显“过去”;
如果($date1==$date2)回显“当前”,则为else;
否则呼应‘未来’;

请参阅文档:

请记住,变量可以被覆盖-就像一年随着季节的推移一样,您的变量也可以被覆盖-只要我们按照顺序进行操作,我们就会得到正确的变量。这意味着我们只需测试日期是否在季节变化的日期之后

// 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;
}

冬季可能会在明年结束。你需要将结束日期设置为次年的3月。你是对的。我已经读过了。但我的问题是,它给了我错误的值。看起来你没有使用季节的实际开始和结束日期,而是取整到月份。所以制作一个数组,映射每个月n数字到季节。strotime()的可能副本是粗略的。看起来这个问题来自欧洲地区,并且意味着日期是DD-MM格式。但是,我不能确定strotime()是否在所有地区都会这样看,而不是YY-MM格式。为了避免问题,我选择mktime,这将确保不会混淆日期和月份。Okej,但这个网站只在瑞典使用!:)换句话说,它将与我编写的代码一起工作?:)对使用strotime代替mktime。它给你一个时间戳。使用时间戳。不要把它改回DD-MM格式。这个很好用!谢谢!:)这可能是最好的,因为它按顺序检查所有不同的时段。谢谢另外,我在作为编辑添加的函数中有一个输入错误,现在已经修复了。
// 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;
}