0

I am trying to determine the difference between the date of a file in a directory and the current date, I have tried calculating two ways:

 $fileUnixTimeDate = filemtime($file);
 $fileFormattedDate = date('m/d/y',filemtime($file));

 $todayUnixTimeDate =  time();
 $todayFormattedDate = date('m/d/y',time());

 $unixDifference = $todayUnixTimeDate - $fileUnixTimeDate;
 $formattedDifference =  $todayFormattedDate - $fileFormattedDate;

This is the result for two files in the directory:

enter image description here

jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34
  • So what is the actual problem? – Mike Brant Jan 23 '13 at 16:40
  • 1
    Might be worth looking into the PHP DateTime class. – Jonnix Jan 23 '13 at 16:41
  • 2
    Consider that your formatted version is subtracting **STRINGS**. `12/01/12 minus 01/23/13` -> `12 minus 01` -> `12-1` = `11`. The unix version is correct - those are simply integers. you get a difference in seconds, which you can trivially convert to minutes/hours/days with some simple math. – Marc B Jan 23 '13 at 16:45

4 Answers4

3

Use PHP's DateTime class - instantiate the two dates as objects of DateTime and do a diff between them. Finally, format that diff to output the days value.

Use http://php.net/DateTime for reference.

EDIT: Example:

$dt1 = new DateTime(date('Y-m-d H:i:s', filemtime($file)));
$dt2 = new DateTime(); // this would be the "now" datetime

$diff = $dt1->diff($dt2);

echo $diff->format('%R%a days');
shadyyx
  • 15,825
  • 6
  • 60
  • 95
2

I can only assume that you're trying to get the number of days' difference:

$fileUnixTimeDate = filemtime($file);
$todayUnixTimeDate =  time();

$unixDifference = $todayUnixTimeDate - $fileUnixTimeDate;
$daysDifference = $unixDifference/86400;

Note: 86400 because there are 86400 seconds in 1 day.

$daysDifference would contain the amount of days.

George
  • 36,413
  • 9
  • 66
  • 103
0

The unix time calculation already do is a great start:

$fileUnixTimeDate = filemtime($file);
$todayUnixTimeDate =  time();
$unixDifference = $todayUnixTimeDate - $fileUnixTimeDate;

Now with the given results (7389045 & 7216242) you need to convert them to a readable format. For example, 7389045 ~= 85.5 days. 7216242 ~= 83.5 days

echo "Hours difference = ".floor((unixDifference )/3600) . "<br>";
echo "Minutes difference = ".floor((unixDifference )/60) . "<br>";
echo "Seconds difference = " .(unixDifference ). "<br>";
echo "Days difference = ".floor((unixDifference )/86400) . "<br>";

Try that and see what results you get.

See this question: Finding days between 2 unix timestamps in php

And more about Unix Time: http://en.wikipedia.org/wiki/Unix_time

Community
  • 1
  • 1
Grambot
  • 4,370
  • 5
  • 28
  • 43
  • 1
    Once you get above 'days' as units your math gets shockingly unreliable if you continue to simply divide by a number of seconds. At that point you need to go to a class like [DateTime](http://ca2.php.net/manual/en/book.datetime.php) that takes into account the varying days in the month, leap years/seconds, etc. – Sammitch Jan 23 '13 at 16:52
  • Apologies, I just copy pasted from the linked article without reviewing it. I'll take it out since you're 100% right. – Grambot Jan 23 '13 at 17:20
0

Using the DateTime class makes it very easy to work with dates:

// Pretend this is from "filemtime()"
$time = strtotime('9 days ago');

// Create a DateTime object using the file's creation time
// Note: Unix timestamps need to be prefixed with "@"
$filetime = new \DateTime('@'.$time);

// The datetime right now, for comparison
$now = new \DateTime('now');

// Get the difference between the two times
$diff = $filetime->diff($now);

// And echo out the day difference
echo "The file was created {$diff->days} days ago.";

The $diff variable contains a lot of good stuff:

object(DateInterval)[3]
    public 'y' => int 0
    public 'm' => int 0
    public 'd' => int 9
    public 'h' => int 0
    public 'i' => int 0
    public 's' => int 0
    public 'invert' => int 0
    public 'days' => int 9
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52