1. filemtime()
作用: 获取文件的修改时间。
语法:
filemtime(string $filename): int|false /* Gets the modification time. Returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed. :param filename: path to the file. :return: the time the file was last modified, or FALSE on failure. The time is returned as a Unix timestamp, which is suitable for the date() function. :errors/exceptions: upon failure, an E_WARNING is emitted. */
e.g.
<?php
echo "last updated:";
echo filemtime('contact.txt');
?>
注意:
1. 每个文件系统的时间分辨率可能会不同。
2. 函数的结果将被存入缓存(cached)。
3. 从PHP 5.0.0开始,此函数也可以用于一些URL wrappers。
效果:
----------------------------------------------------
Reference:
1. https://www.php.net/manual/en/function.filemtime.php
缺点:
该函数返回的是Unix Time Stamp时间戳类型,以秒为单位追溯时间(reference: https://www.unixtimestamp.com/),不够直观。
解决方法:
用date()函数指定输出的时间格式(reference: https://www.php.net/manual/en/function.date.php
2. date()
作用:输出指定格式的时间。
语法:
date(string $format[, int $timestamp = time()]) : string /* Format a local time/date. Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. :param format: format accepted by DateTimeInterface::format(). :timestamp: The optional timestamp parameter is an int Unix timestamp that defaults to the current local time if a timestamp is not given. :return: a formatted date string. :errors/exceptions: if a non-numeric value is used for timestamp, FALSE is returned and an E_WARNING level error is emitted. Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable. */
e.g.
<?php
echo "last updated:";
echo date ("F d Y H:i:s", filemtime('contact.txt'));
?>
注意:
1. timestamp是一个可选的函数,如用户未指定,则其值等于系统的当前时间,即等于函数time()的返回值。
效果:
----------------------------------------------------
Reference:
1. https://www.php.net/manual/en/function.date.php
3. diffForHumans()
作用:用文字方式表述时间。
e.g.
<?php require "libs/Carbon.php"; use CarbonCarbon; $ts = filemtime($filename); // 创建时间戳 $carb = Carbon::createFromTimestamp($ts); // 创建Carbon对象 echo "last updated:"; echo $carb->diffForHumans(); // 生成文字内容 ?>
注意:
1. 引用前需先下载库文件Carbon.php(https://raw.githubusercontent.com/briannesbitt/Carbon/1.17.0/src/Carbon/Carbon.php)。Carbon是继承自PHP DateTime()的子类,但比后者提供了更丰富、更语义化的API(reference: https://learnku.com/articles/5577/the-diffforhumans-method-of-carbon。
效果: