PHP检查数组中的多个日期是否在日期范围内 [英] php check multiple dates in array are within a date range

查看:383
本文介绍了PHP检查数组中的多个日期是否在日期范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下的数组:

I have an array structured like this:

Array ( [0] => 24-12-2013 [1] => 25-12-2013 [2] => 26-12-2014 [3] => 27-12-2013 [4])

我想检查数组中的任何日期是否在给定的日期范围内。

I would like to check if any of the dates in the array are within a given date range.

日期范围的结构如下:

$start = (date("d-m-Y", strtotime('25-12-2013')));
$end =   (date("d-m-Y", strtotime('26'12'2013')));

我想知道数组中哪些日期在日期范围内。

I would like to know which dates in the array are within the date range.

推荐答案

夫妇:


  • 使用时间戳或 DateTime 对象来比较日期,而不是字符串

  • 使用日期格式YYYY-MM-DD来避免日期格式的潜在歧义(d / m / y或m / d / y)

  • Use timestamps or DateTime objects to compare dates, not strings
  • Use date format YYYY-MM-DD to avoid potential ambiguity about your date format (d/m/y or m/d/y)

此代码将执行您想要的操作:

This code will do what you want:

$dates = array("2013-12-24","2013-12-25","2014-12-24","2013-12-27");
$start = strtotime('2013-12-25');
$end =   strtotime('2013-12-26');

foreach($dates AS $date) {
    $timestamp = strtotime($date);
    if($timestamp >= $start && $timestamp <= $end) {
        echo "The date $date is within our date range\n";
    } else {
        echo "The date $date is NOT within our date range\n";
    }
}

查看实际操作:

http://3v4l.org/GWJI2

这篇关于PHP检查数组中的多个日期是否在日期范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆