-1

how can i display e.g. Today or Yesterday if a file was changed today or yesterday instead of the date with filemtime ?

snipped:

$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);

Thanks!

Stan
  • 129
  • 12

2 Answers2

2

Try this

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";
}
Kerkouch
  • 1,446
  • 10
  • 14
1

You are almost there. Just need to add a formatter to filemtime and compare to the today and yesterday dates:

$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';
}
Jim
  • 3,210
  • 2
  • 17
  • 23