Bijan
Bijan

Reputation: 8586

PHP: Showing How Long Ago A File Was Modified

I am using the following code to show how long ago a file was last modified

echo gmdate("d\d H\h i\m s\s", time()-filemtime(FILENAME));

This will result in something like 00d 00h 15m 28s to show a file was last modified 15m ago.

I am trying to limit how much time I can show if it will just be zeros. I.e. instead of 00d 00h 15m 28s I want it to just show 15m 28s.

Is there a simple way to do this?

Upvotes: 3

Views: 832

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

I think you can use a DateTime and use the setTimestamp method to set the timestamp from filemtime.

For example:

<?php
$dateTime = new DateTime();
$dateTimeFile = new DateTime();
$dateTimeFile->setTimestamp(filemtime(FILENAME));
$interval = $dateTime->diff($dateTimeFile);
echo rtrim(
    sprintf(
        "%s%s%s%s",
        $interval->d > 0 ? $interval->d . "d " : "",
        $interval->h > 0 ? $interval->h . "h " : "",
        $interval->i > 0 ? $interval->i . "m " : "",
        $interval->s > 0 ? $interval->s . "s " : ""
    )
);

Upvotes: 5

jankal
jankal

Reputation: 1128

It's not really what you want...

I'm currently using this:

define('MINUTE_IN_SECONDS', 60);
define('HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS);
define('DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS);
define('WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS);
define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);

function humanTimeDifference($from, $to = '') {
    if ( empty( $to ) )
        $to = time();
    $diff = (int) abs( $to - $from );
    if ( $diff < HOUR_IN_SECONDS ) {
        $mins = round( $diff / MINUTE_IN_SECONDS );
        if ( $mins <= 1 )
            $mins = 1;
        $since = sprintf( _n( '%s minute', '%s minutes', $mins ), $mins );
    } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
        $hours = round( $diff / HOUR_IN_SECONDS );
        if ( $hours <= 1 )
            $hours = 1;
        $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
    } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
        $days = round( $diff / DAY_IN_SECONDS );
        if ( $days <= 1 )
            $days = 1;
        $since = sprintf( _n( '%s day', '%s days', $days ), $days );
    } elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
        $weeks = round( $diff / WEEK_IN_SECONDS );
        if ( $weeks <= 1 )
            $weeks = 1;
        $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
    } elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
        $months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
        if ( $months <= 1 )
            $months = 1;
        $since = sprintf( _n( '%s month', '%s months', $months ), $months );
    } elseif ( $diff >= YEAR_IN_SECONDS ) {
        $years = round( $diff / YEAR_IN_SECONDS );
        if ( $years <= 1 )
            $years = 1;
        $since = sprintf( _n( '%s year', '%s years', $years ), $years );
    }
    return $since;
}

function _n($single, $plural, $number) {
    if($number > 1) {
        return $plural;
    } else {
        return $single;
    }
}

I just took it from my GitHub project

Upvotes: 3

Related Questions