5

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.

jcobhams
  • 796
  • 2
  • 12
  • 29
  • `if(strtotime($d1)-(60*60*24) <= time()-(60*60*24) || strtotime($d1) < strtotime($d2))` is something of what your looking for. – Sammaye Oct 12 '12 at 09:03
  • I bet this has been asked and answered already. Please use the search first. – hakre Oct 15 '12 at 10:41

6 Answers6

8

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 {
 }
Kalpesh Patel
  • 2,772
  • 2
  • 25
  • 52
4

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
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • how do i tweak it to check it its not up to three days from last modified. – jcobhams Oct 12 '12 at 09:14
  • @VyrenMedia I'm not sure I understand ... "not up to three days" translates to "at least three days" ... so my answer is just one second off? If you compare using `<=` you should have what you want I think. – Ja͢ck Oct 12 '12 at 09:29
3

Just check it with timestamp:

if (time() - filemtime($testfile) >= 3 * 86400) {
  // ...
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
1

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

simply-put
  • 1,068
  • 1
  • 11
  • 20
1

well, stunned to see no one is using mktime() function, it makes the job simple

for example your input date is :10/10/2012

mktime convert it to unix time stamp

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

we can perform any operations weather +,-,*,/

Manoj
  • 373
  • 3
  • 10
  • i dont understand how this tries to solve my problem..please explain your code and how i solves my problem – jcobhams Oct 15 '12 at 11:20
  • mktime(). function converts the time given into a unix time stamp. here in my example i am adding +3days to current days – Manoj Oct 15 '12 at 11:33
0

use timestamp instead of date,

$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
  ..
}
Teson
  • 6,644
  • 8
  • 46
  • 69