Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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检查日期是今天还是未来7天_Php_Date_Strtotime - Fatal编程技术网

PHP检查日期是今天还是未来7天

PHP检查日期是今天还是未来7天,php,date,strtotime,Php,Date,Strtotime,我试图解决一个基本问题。我想检查用户是否输入了有效日期。我们申请的有效日期为今天或以后7天。因此,该范围内的任何日期都是有效的。过去或从现在起第7天起的任何日期都将被视为无效 我编写了一个小函数来解决这个问题: function is_valid($d) { if( strtotime($d) < strtotime('+7 days') ) { return true; } else return false; } Usage : is_valid('01-20-2015'); //M

我试图解决一个基本问题。我想检查用户是否输入了有效日期。我们申请的有效日期为今天或以后7天。因此,该范围内的任何日期都是有效的。过去或从现在起第7天起的任何日期都将被视为无效

我编写了一个小函数来解决这个问题:

function is_valid($d)
{
if( strtotime($d) < strtotime('+7 days') ) {
return true;
}
else return false;
}
Usage : is_valid('01-20-2015');  //M-D-Y
函数有效($d)
{
如果(标准时间($d)<标准时间('+7天')){
返回true;
}
否则返回false;
}
用法:有效('01-20-2015')//M-D-Y
但这总是回到了现实

我做错了什么


Ahmar无法解析该日期格式。您需要使用YYYY-MM-DD

使用本测试代码进行确认

<?php
function is_valid($d) {
   if (strtotime($d) < strtotime('+7 day')) {
      return true;
   }
   else 
      return false;
}

var_dump(is_valid('2015-12-25'));

// Wrong format;
echo "Right: " . strtotime('2015-12-25') . "\n";

// Right format;
echo "Wrong: " . strtotime('01-20-2015') . "\n";

<代码> 在注释中被引用-你不考虑<代码> SttoTime[()> <代码>无法解析所输入的日期(无效日期格式等)。

请尝试以下代码:

function is_valid($d)
{
   if( strtotime($d) !== FALSE && strtotime($d) < strtotime('+7 days') ) {
      return true;
   }
   return false;
}
Usage : is_valid('01-20-2015');  //M-D-Y
函数有效($d)
{
if(strotime($d)!==FALSE&&strotime($d)

您还应该记住,
strotime
取决于服务器的时区,如下所示

使用DateTime函数要容易得多

$yourDate = new \DateTime('01-20-2015');

// Set 00:00:00 if you want the aktual date remove the setTime line
$beginOfDay = new \DateTime();
$beginOfDay->setTime(0,0,0);

// Calculate future date
$futureDate = clone $beginOfDay;
$futureDate->modify('+7 days');

if($yourDate > $beginOfDay && $yourDate < $futureDate) {
    // do something
}
$yourDate=new\DateTime('01-20-2015');
//设置00:00:00如果需要实际日期,请删除设置时间行
$beginOfDay=new\DateTime();
$beginOfDay->setTime(0,0,0);
//计算未来日期
$futureDate=克隆$beginOfDay;
$futureDate->修改(“+7天”);
如果($yourDate>$beginOfDay&$yourDate<$futureDate){
//做点什么
}

strotime()
可能无法明确解析类似
01-20-2015
的格式。它很可能返回
(bool)false
如何修复@Michaelberkowski您是否尝试过使用Y-M-D格式?下面给出了答案;咨询他们,让他们知道这是否有效。