Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find difference between now and a file's last change time in PHP

Tags:

php

I have a file called "file.txt". I get its last change time with this:

$lastTime = filemtime("file.txt");

Then I make

$lastDate = date("Y-m-d",strtotime( $lastTime ));
$todaysDate = date("Y-m-d",strtotime('now'));

Then I substract lastDate from todaysDate and find the difference. Is there a quicker way to do this?

I don't want to check whether the difference of seconds is >= 86400 or not.

For example the difference between these two dates must be "1".

2013-03-31 10:00
2013-03-30 19:00

If i check for the difference of seconds I will have 54K seconds, which is smaller than 86400 seconds.

like image 296
trante Avatar asked Dec 27 '22 07:12

trante


1 Answers

Get the difference in seconds, then convert to days and round the result.

$diff = time()-filemtime("file.txt");
$days = round($diff/86400);

You could also use floor or ceil in place of round depending on exactly how you want to handle partial days.

like image 130
Mark Parnell Avatar answered Jan 13 '23 14:01

Mark Parnell