1

I'm trying to get the difference between the current time and the filemtime in PHP.

The time and filemtime seems to return correct values, since, when applied to the date function, these values print correct dates. The difference between the time() function and filemtime is 1 hour according to the date function. However when I'm trying to get the difference between the returned timestamp values, the difference is 215. For example, see below:

current time = 1339599628, the date() function shows 13-06-2012 16:06:28 00 file modif time = 1339599413, the date() function shows 13-06-2012 15:06:30 30

This seems to be correct, but I can't see how to check the difference in seconds between these dates, since 1 hours has 3600 seconds, but the difference is only 215.

Also, I tried the following timestamp values: 1339599599 = 13-06-2012 15:06:59 59 1339599600 = 13-06-2012 16:06:00 00

So, the first timestamp values was incremented by 1, but the date gives 1 hours difference.

Any ideas?

1
  • Maybe you just hit the change from winter time to summer time? When that happens, the real time span is just one second, but we adjust our clocks to 1hour of difference Commented Jun 14, 2012 at 8:36

4 Answers 4

3

You seem to have a problem with timezone difference. Try to look at gmdate function.

Or better, before you get these timestamps, call date_default_timezone_set('UTC').

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but this didn't help. For instance, try this one: echo date('d-m-Y H:m:s i', 1339599599).'<br>';//13-06-2012 15:06:59 59 echo date('d-m-Y H:m:s i', 1339599600).'<br>';//13-06-2012 16:06:00 00 When I increment the first timespamt by 1, the date is +1hour. Also I tryed this one: $time = mktime(15, 30, 30, 6, 13, 12); echo date('d-m-Y H:m:s i', $time); Thei should be 13-06-2012 15:30:30, but it gives: 13-06-2012 15:06:30 30.
@Andrei Have you tried to reset the timezone before you call both time() and filemtime() to get these stamps? You should get different numbers. In case of your mktime() - gmmktime() would also give you different results.
Ye, I tried to reset, but still the same. What I'm trying to do, is to remove all files that were not acessed in the last 25 minutes. I'm using the time() function to get the current time stamp and filemtime() to get the file mod time. As I understand, the following code should do the treak: if($fileModTime - $currentTime > 25*60) unlink($file); But this doesn't work.
@Andrei what configuration are you running? This old bug might be related.
It works on Win 7, but I tried on linux and doesn't work. Config: Apache 2.2.22, PHP 5.3.10, architecture: x86_64.
|
1

I had the same problem, in my case the time difference between time() and filemtime() was 51 seconds. I have no idea what causes this, but I did find a workaround. When a file is created I use touch($file, time()). Now the creation time of the created file is set "in sync" with time().

Comments

0

I don't really understand your question... both time and filemtime returns timestamps.

biggestOne - smallestOne = diff in seconds. There shouldn't be anything more to it.

4 Comments

It doesn't even matter what order you put them in the calculation - it is just $difference = abs($timeStamp1 - $timeStamp2);
it does not matter as long as you use abs :) The point is just to know what is problem really is...
The problems seems to be in the returned values. The difference between time() and filemtime() is 215. But when applied to the data function, these values give 1 hour difference.
This code works perfectly on mi side. However, the following doesn't work: echo date('d-m-Y H:m:s i', 1339599599).'<br>';//13-06-2012 15:06:59 59 echo date('d-m-Y H:m:s i', 1339599600).'<br>';//13-06-2012 16:06:00 00
0

I recently had the same issue, after checking the system side, I found that there is a difference between the system time and the time provided by the stat command that shows the last modified time.

-bash$ date;touch test;stat test;date
Sat Aug 22 08:06:35 BRT 2015
  File: `test'
  Size: 0               Blocks: 1          IO Block: 32768  regular empty file
Device: 13h/19d Inode: 25189989    Links: 1
Access: (0664/-rw-rw-r--)  Uid: (/)   Gid: (/)
Access: 2015-08-22 08:07:29.968227010 -0300
Modify: 2015-08-22 08:07:29.968227064 -0300
Change: 2015-08-22 08:07:29.968238305 -0300
Sat Aug 22 08:06:35 BRT 2015

The difference in my case is 53sec.

Other machines I tested didn't show this difference.

Tested on a hosting machine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.