简体   繁体   中英

PHP - time and filemtime issue

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?

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') .

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().

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM