如何检查日期是否在今天前三天



嘿,我想知道是否有任何脚本(php)可以检查今天前三天的指定日期。

说。。

$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");

现在我想知道如何比较这两个日期以检查 D1 是否至少在 3 天前或 D2 之前任何帮助将不胜感激。

为什么不使用 DateTime 对象。

 $d1 = new DateTime(date('Y-m-d',filemtime($testfile));
 $d2 = new DateTime(date('Y-m-d'));
 $interval = $d1->diff($d2);
 $diff = $interval->format('%a');
 if($diff>3){
 }
 else {
 }

假设您希望测试文件是否在三天前被修改:

if (filemtime($testfile) < strtotime('-3 days')) {
   // file modification time is more than three days ago
}

只需使用时间戳检查它:

if (time() - filemtime($testfile) >= 3 * 86400) {
  // ...
}

使用date("Y-m-d", strtotime("-3 day"));表示特定日期

您也可以使用

strtotime(date("Y-m-d", strtotime("-3 day")));

在比较日期字符串之前将其转换为整数

好吧,惊讶地发现没有人在使用 mktime() 函数,它使工作变得简单

例如,您的输入日期是:10/10/2012

MKtime将其转换为UNIX时间戳

$check_date=mktime(0,0,0,10,**10+3**,2012);

我们可以执行任何操作天气+,-,*,/

使用时间戳而不是日期,

$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
  ..
}

最新更新