简体   繁体   English

PHP - 时间和文件时间问题

[英]PHP - time and filemtime issue

I'm trying to get the difference between the current time and the filemtime in PHP. 我试图在PHP中获取当前时间和filemtime之间的差异。

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. 根据日期函数,time()函数和filemtime之间的差异为1小时。 However when I'm trying to get the difference between the returned timestamp values, the difference is 215. For example, see below: 但是,当我试图获取返回的时间戳值之间的差异时,差异为215.例如,请参见下文:

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 当前时间= 1339599628,日期()功能显示13-06-2012 16:06:28 00文件修改时间= 1339599413,日期()功能显示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. 这似乎是正确的,但我看不出如何检查这些日期之间的秒数差异,因为1小时有3600秒,但差异只有215。

Also, I tried the following timestamp values: 1339599599 = 13-06-2012 15:06:59 59 1339599600 = 13-06-2012 16:06:00 00 另外,我尝试了以下时间戳值: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. 因此,第一个时间戳值增加1,但日期给出1小时的差异。

Any ideas? 有任何想法吗?

You seem to have a problem with timezone difference. 你似乎有时区差异的问题。 Try to look at gmdate function. 试着看看gmdate函数。

Or better, before you get these timestamps, call date_default_timezone_set('UTC') . 或者更好的是,在获得这些时间戳之前,请调用date_default_timezone_set('UTC')

I had the same problem, in my case the time difference between time() and filemtime() was 51 seconds. 我有同样的问题,在我的情况下,time()和filemtime()之间的时差是51秒。 I have no idea what causes this, but I did find a workaround. 我不知道是什么导致这种情况,但我确实找到了解决方法。 When a file is created I use touch($file, time()) . 创建touch($file, time())我使用touch($file, time()) Now the creation time of the created file is set "in sync" with time(). 现在,创建文件的创建时间与time()设置为“同步”。

I don't really understand your question... both time and filemtime returns timestamps. 我真的不明白你的问题......时间和文件时间都会返回时间戳。

biggestOne - smallestOne = diff in seconds. largestOne - smallestOne = diff,以秒为单位。 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. 我最近遇到了同样的问题,在检查系统端之后,我发现系统时间stat命令提供的显示上次修改时间的时间之间存在差异。

-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. 我的情况有所不同是53秒。

Other machines I tested didn't show this difference. 我测试的其他机器没有显示出这种差异。

Tested on a hosting machine. 在主机上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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