Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a date is three days before today

Tags:

date

php

datetime

Hey i would like to know if there is any script (php) that could check if a specified date three days before today.

say..

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

now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2 any help would be gladly appreciated.

like image 214
jcobhams Avatar asked Oct 12 '12 09:10

jcobhams


4 Answers

Why not to use DateTime object.

 $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 {
 }
like image 197
Kalpesh Patel Avatar answered Sep 23 '22 01:09

Kalpesh Patel


Assuming you wish to test whether the file was modified more than three days ago:

if (filemtime($testfile) < strtotime('-3 days')) {
   // file modification time is more than three days ago
}
like image 24
Ja͢ck Avatar answered Sep 24 '22 01:09

Ja͢ck


Just check it with timestamp:

if (time() - filemtime($testfile) >= 3 * 86400) {
  // ...
}
like image 27
xdazz Avatar answered Sep 25 '22 01:09

xdazz


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

you can also use

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

to convert it to integer before comparing a date string

like image 44
simply-put Avatar answered Sep 26 '22 01:09

simply-put