简体   繁体   English

如何获得与当前时间相比的文件时间

[英]How to get filemtime compared to current time

I need to check the last modified time of my file.我需要检查我的文件的最后修改时间。 I have used filemtime() in PHP for that as below..我在 PHP 中使用filemtime() ,如下所示。

 <h6 style="text-align: right" > <?php echo "Content last changed: ".date("F d Y H:i:s.", filemtime("file.js"));?> </h6>

This works fine and I could see the modified time.. Now I need to get this time compared to current time..这工作正常,我可以看到修改后的时间..现在我需要得到这个时间与当前时间相比..

As an example , if the file modified 30 seconds ago, it should show Content last changed:30 seconds if the file modifies 5 minutes ago, it should show Content last changed:5 minutes , if the file modified 2 hours ago, it should show accordingly.例如,如果文件在 30 秒前修改,则应显示Content last changed:30 seconds如果文件在 5 分钟前修改,则应显示Content last changed:5 minutes ,如果文件在 2 小时前修改,则应显示因此。

Since I am using this method in so many places , I would like to have a function rather than inline code.由于我在很多地方都使用这种方法,所以我想要一个函数而不是内联代码。

Can someone help me on this,有人可以帮我解决这个问题,

Thanks In advance!提前致谢!

This function gets the time difference in seconds and then converts it to the format 0 hours 0 minutes 0 seconds (0 is used for reference).该函数以秒为单位获取时差,然后将其转换为0 hours 0 minutes 0 seconds的格式(0 用于参考)。

function func( $file ) {
    $seconds = time() - filemtime( $file );
    $hours = floor($seconds / 3600);
    $minutes = floor(($seconds / 60) % 60);
    $seconds = $seconds % 60;
    if ( $hours != 0 ) {
        return $hours . ' hours';
    }
    if ( $minutes != 0 ) {
        return $minutes . ' minutes';
    }
    if ( $seconds ) {
        return $seconds . ' seconds ';
    }
}

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

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