PHP检查两个日期之间的日期

时间:2022-12-20 08:38:39

I got this code from * and changed it slightly to work with today's date.

我从*获得了这个代码并稍微改变它以适应今天的日期。

I want to check if today fits between two dates. But this is not working. What am I missing?

我想检查今天是否适合两个日期。但这不起作用。我错过了什么?

$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today! 
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');

if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
{
  echo "is between";
}
else
{
echo "NO GO!";  
}

7 个解决方案

#1


109  

This is the right answer for your code.. Just use strtotime() php function

这是你的代码的正确答案..只需使用strtotime()php函数

    $paymentDate = date('Y-m-d');
    $paymentDate=date('Y-m-d', strtotime($paymentDate));;
    //echo $paymentDate; // echos today! 
    $contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
    $contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

    if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd))
    {
      echo "is between";
    }
    else
    {
      echo "NO GO!";  
    }

#2


22  

You cannot compare date-strings. It is good habit to use PHP's DateTime object instead:

您无法比较日期字符串。改为使用PHP的DateTime对象是个好习惯:

$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today! 
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd  = new DateTime('2015-01-01');

if (
  $paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() && 
  $paymentDate->getTimestamp() < $contractDateEnd->getTimestamp()){
  echo "is between";
}else{
   echo "NO GO!";  
}

#3


13  

If hours matter:

如果时间重要:

$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");

if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd) {
   echo "is between";
} else {
    echo "NO GO!";  
}    

#4


4  

You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20 would appear after a line 100 because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).

您将日期作为字符串进行比较,这将无法正常工作,因为比较是字典的。这与排序文本文件时的问题相同,其中第20行将出现在第100行之后,因为内容不被视为数字而是被视为ASCII代码序列。此外,创建的日期都是错误的,因为您使用的字符串格式字符串需要时间戳(第二个参数)。

Instead of this you should be comparing timestamps of DateTime objects, for instance:

您应该比较DateTime对象的时间戳,而不是这个,例如:

 $paymentDate = date_create();
 $contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
 $contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');

Your existing conditions will then work correctly.

您现有的条件将正常工作。

#5


0  

Use directly

直接使用

$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");

Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.

然后比较就可以了,因为01-01-2015对于PHP的32位日期范围是有效的,在strtotime的手册中有说明。

#6


0  

Based on luttken's answer. Thought I'd add my twist :)

根据luttken的回答。以为我会添加我的扭曲:)

function dateIsInBetween(\DateTime $from, \DateTime $to, \DateTime $subject)
{
    return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
}

$paymentDate       = new \DateTime('now');
$contractDateBegin = new \DateTime('01/01/2001');
$contractDateEnd   = new \DateTime('01/01/2016');

echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";

#7


0  

Another solution would have been to consider date written as Ymd.

另一种解决方案是将日期写为Ymd。

Written in this "format" this is much easy to compare dates.

用这种“格式”写的这很容易比较日期。

$paymentDate       = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin     = 20010101;
$contractEnd       = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";

It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate to compare with the int value of contractBegin and contractEnd).

它将始终适用于一年中的每一天,并且不依赖于任何函数或转换(PHP将考虑$ paymentDate的int值以与contractBegin和contractEnd的int值进行比较)。

#1


109  

This is the right answer for your code.. Just use strtotime() php function

这是你的代码的正确答案..只需使用strtotime()php函数

    $paymentDate = date('Y-m-d');
    $paymentDate=date('Y-m-d', strtotime($paymentDate));;
    //echo $paymentDate; // echos today! 
    $contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
    $contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

    if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd))
    {
      echo "is between";
    }
    else
    {
      echo "NO GO!";  
    }

#2


22  

You cannot compare date-strings. It is good habit to use PHP's DateTime object instead:

您无法比较日期字符串。改为使用PHP的DateTime对象是个好习惯:

$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today! 
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd  = new DateTime('2015-01-01');

if (
  $paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() && 
  $paymentDate->getTimestamp() < $contractDateEnd->getTimestamp()){
  echo "is between";
}else{
   echo "NO GO!";  
}

#3


13  

If hours matter:

如果时间重要:

$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");

if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd) {
   echo "is between";
} else {
    echo "NO GO!";  
}    

#4


4  

You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20 would appear after a line 100 because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).

您将日期作为字符串进行比较,这将无法正常工作,因为比较是字典的。这与排序文本文件时的问题相同,其中第20行将出现在第100行之后,因为内容不被视为数字而是被视为ASCII代码序列。此外,创建的日期都是错误的,因为您使用的字符串格式字符串需要时间戳(第二个参数)。

Instead of this you should be comparing timestamps of DateTime objects, for instance:

您应该比较DateTime对象的时间戳,而不是这个,例如:

 $paymentDate = date_create();
 $contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
 $contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');

Your existing conditions will then work correctly.

您现有的条件将正常工作。

#5


0  

Use directly

直接使用

$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");

Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.

然后比较就可以了,因为01-01-2015对于PHP的32位日期范围是有效的,在strtotime的手册中有说明。

#6


0  

Based on luttken's answer. Thought I'd add my twist :)

根据luttken的回答。以为我会添加我的扭曲:)

function dateIsInBetween(\DateTime $from, \DateTime $to, \DateTime $subject)
{
    return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
}

$paymentDate       = new \DateTime('now');
$contractDateBegin = new \DateTime('01/01/2001');
$contractDateEnd   = new \DateTime('01/01/2016');

echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";

#7


0  

Another solution would have been to consider date written as Ymd.

另一种解决方案是将日期写为Ymd。

Written in this "format" this is much easy to compare dates.

用这种“格式”写的这很容易比较日期。

$paymentDate       = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin     = 20010101;
$contractEnd       = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";

It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate to compare with the int value of contractBegin and contractEnd).

它将始终适用于一年中的每一天,并且不依赖于任何函数或转换(PHP将考虑$ paymentDate的int值以与contractBegin和contractEnd的int值进行比较)。