在人类日期格式化filemtime()

时间:2017-09-20 16:03:35

标签: php date filemtime

我该如何显示,例如今天或昨天如果今天或昨天更改了文件而不是filemtime的日期?

剪断:

$date = date('d-m-Y', $timestamp);
$today = date('d-m-Y');
$yesterday = date('d-m-Y', strtotime('yesterday')); 
$otherdate= get_the_date();

$file= 'index.php';
$time= filemtime ( $file);

谢谢!

2 个答案:

答案 0 :(得分:2)

试试这个

define( 'DAY_IN_SECONDS', 24*60*60 );

$file = 'index.php';
$time = filemtime( $file );
$today_start_at = strtotime(date('Y-m-d'));
$yesterday_start_at = $today_start_at - DAY_IN_SECONDS;

if( ($time - $today_start_at) >= 0 ){
    // Today
    echo 'Today';
}elseif ( ($time - $yesterday_start_at) >= 0 ) {
    // Yesterday
    echo 'Yesterday';
}else{
    // N days ago
    $days_ago = floor( (time() - $time)/DAY_IN_SECONDS );
    echo "{$days_ago} days ago";
}

答案 1 :(得分:1)

你快到了。只需要将格式化程序添加到filemtime并与今天和昨天的日期进行比较:

$date = date('d-m-Y', $timestamp);
$today = date('d-m-Y');
$yesterday = date('d-m-Y', strtotime('yesterday')); 
$otherdate= get_the_date();

$file= 'index.php';
$time= date('d-m-Y', filemtime($file));

if( $time == $today ) {
    echo 'Modified Today';
} else if( $time == $yesterday ) {
    echo 'Modified Yesterday';
}