PHP 检查日期是否在两个日期之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19070116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP check if date between two dates
提问by Papa De Beau
I got this code from Stackoverflow and changed it slightly to work with today's date.
我从 Stackoverflow 获得了这段代码,并对其进行了轻微更改以使用今天的日期。
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!";
}
回答by g.m.ashaduzzaman
Edit:use <= or >= for count todays date
编辑:使用 <= 或 >= 计算今天的日期
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!";
}
回答by luttkens
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!";
}
回答by Cristiano Gois de Araújo
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!";
}
回答by Vigintas Labakojis
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!";
回答by Jon
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.
然后您现有的条件将正常工作。
回答by ChtiSeb
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 将考虑的 int 值$paymentDate
与contractBegin
and的 int 值进行比较contractEnd
)。
回答by Jim Turner
If you need bracket dates to be dynamic ..
如果您需要括号日期是动态的..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
$period="summer";
}else{
$period="winter";
}
回答by Adam Whateverson
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
另一个解决方案(假设您知道您的日期格式始终是 YYYY/MM/DD,带有前导零)是 max() 和 min() 函数。我认为这是可以的,因为所有其他答案也假设 yyyy-mm-dd 格式,如果您想确保它们按日期顺序排序,这是文件系统中文件夹的通用命名约定。
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
正如其他人所说,鉴于数字的顺序,您可以比较字符串,不需要 strtotime() 函数。
Examples:
例子:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
优点是您可以在其中放置更多日期,而不仅仅是比较两个日期。
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare the result of min() and max()
要确定日期是否在两个日期之间,您可以比较 min() 和 max() 的结果
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check)){
// It's in the middle
}
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
它不适用于任何其他日期格式,但它适用于那种格式。无需转换为秒,也无需日期函数。
回答by pohankar gaurang
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing . Here is the code for the same:
上述方法很有用,但它们不是完全证明,因为如果时间在 12:00 AM/PM 和 01:00 AM/PM 之间,它会出错。尽管在时间之间,它仍将返回“No Go”。这是相同的代码:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
{
echo "is between";
}
else
{
echo "NO GO!";
}
Here you will get output "NO Go"
because 12:45 > 01:45
.
在这里你会得到输出,"NO Go"
因为12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
要在两者之间获得正确的输出日期,请确保"AM"
来自01:00 AM
to 的值12:00 AM
将转换为 24 小时格式。这个小技巧帮助了我。
回答by Spock
Simple solution:
简单的解决方案:
function betweenDates($cmpDate,$startDate,$endDate){
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
}