php - 时间段中的时间段 (PHP)

标签 php mysql time intervals

我试图找出一个时间段是否在一个时间段内。我有我的引用时间段和我的比较时间段。 我举个例子:

  • 时间段 A(引用)从 2014 年 1 月 1 日到 2014 年 2 月 1 日 (tt.mm.yyyy)。
  • 时间段 B(比较)从 2014 年 4 月 1 日到 2014 年 5 月 1 日。

=> 这完全没问题。

  • 时间段 C(引用)为 2014 年 1 月 1 日至 2014 年 3 月 1 日
  • 时间段 D(比较)从 2014 年 2 月 1 日到 2014 年 5 月 1 日。

=> 不行,因为 D 在 C 中。

希望你能得到我想要的。我尝试使用 serval < = > if 操作,但这开始变得巨大且缓慢。也许有更快的方法来做到这一点。

还有,MySQL 能做这样的事情吗?

最佳答案

你可以用php时间戳来尝试这个

$reference_start_date = "1.1.2014";
$reference_end_date = "1.2.2014";

$comparative_start_date = "1.4.2014";
$comparative_end_date = "1.5.2014 ";

$reference_start_time = strtotime(str_replace(".", "-", $reference_start_date);
$reference_end_time = strtotime(str_replace(".", "-", $reference_end_date);

$comparative_start_time = strtotime(str_replace(".", "-", $comparative_start_date);
$comparative_end_time = strtotime(str_replace(".", "-", $comparative_end_date);

if($comparative_start_time>$reference_start_time && $comparative_start_time<$reference_end_time)
{
    echo "Not OK";
}
else if($comparative_end_time>$reference_start_time && $comparative_end_time<$reference_end_time)
{
    echo "Not OK";
}
else if($comparative_start_time<$reference_start_time && $comparative_end_time>$reference_end_time)
{
     echo "Not OK";
}
else
{
     echo "OK";
}

关于php - 时间段中的时间段 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839380/

相关文章:

java - SimpleDateFormat 不工作

javascript - 使用 Ajax 检查登录表单

php - 如何修复向 MySQL 发送查询的问题? PHP 中的错误

php - mysql 显示表中添加的所有行

c - C 中耗时

javascript - 如何在 JavaScript 中比较时间(仅小时)?

php - 可捕获的 fatal error : Object of class "" could not be converted to string in "" on line ""

mysql - 根据另一个表中的主键查找设置新列值

php - 数据库中的日期格式与输出不同

mysql - 如何在另一个表中使用数据类型字符串作为外键创建无主键