Search code examples
phpdatefilemtime

Format filemtime() in human date


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!


Solution

  • 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';
    }