PHP time elapsed, remove "and" before seconds

350 views Asked by At

So, I've got this working code which converts a MySQL date to UNIX Timestamp and subtracts it from the current date() to show a "time elapsed since X"-like timer. (the part which takes the date from database is missing since it's in another script)

    <?php 
    function time_elapsed($secs){
        $bit = array(
            ' year'        => $secs / 31556926 % 12,
            ' week'        => $secs / 604800 % 52,
            ' day'        => $secs / 86400 % 7,
            ' hour'        => $secs / 3600 % 24,
            ' minute'    => $secs / 60 % 60,
            ' second'    => $secs % 60
            );
        foreach($bit as $k => $v){
            if($v > 1)$ret[] = $v . $k . 's';
            if($v == 1)$ret[] = $v . $k;
            }
        array_splice($ret, count($ret)-1, 0, 'and');
        $ret[] = 'ago.';

        return join(' ', $ret);
        }

    $nowtime = time() + 10; //add 10s to avoid error
    $oldtime = strtotime($mysqltime2);

    $time_elapsed = time_elapsed($nowtime-$oldtime)."\n";
    echo wordwrap($time_elapsed,35,"<br />\n"); //split long line
    ?>

I managed to fix an error reporting missing array or something if the script was executed at the same time of the MySQL date by adding 10 seconds to the current timestamp.

Another issue I have is that the script shows "and X seconds" even if there aren't minutes/hours/days/etc before it. E.g. "and X seconds" "Y minutes and X seconds" "Z hours Y minutes and X seconds" "N days Z hours Y minutes and X seconds"

I want to remove the "and" before seconds ONLY if there's no minutes/hours/etc before it.

Any tip on how to fix this?

3

There are 3 answers

1
TiiJ7 On BEST ANSWER

You could only splice when needed:

if( count($ret) > 1 ) {
    array_splice($ret, count($ret)-1, 0, 'and');
}
3
TecBrat On

The simplest I can think of is

$time_elapsed = time_elapsed($nowtime-$oldtime))."\n";
if($nowtime-$oldtime < 61){
  $time_elapsed=str_replace('and ',' ',$time_elapsed);
}
2
KevInSol On

Not sure if this will help, but why not do most of the date maths in mysql?

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Here is something I did before to list posts in a comments section of a site, it outputs "post made 2 hours ago" etc

SQL:

UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(made_on)  as madeon 

PHP (pass madeon from query to here):

function seconds_to_textual_time_ago($seconds)
    {
    $hour = 60* 60;
    $day = 24 * $hour;
    $month = 30 * $day;
    $year = 365 * $day;

    switch(true) 
        {
        case ($seconds < 60) :
            $time_ago = "Less than 1 minute ago";
            break;

        case ($seconds >= 60 && $seconds < $hour):
            $minutes = floor($seconds /60);
            if ($minutes > 1) 
                $time_ago = "$minutes minutes ago";
            else
                $time_ago = "1 minute ago";
            break;



        case ($seconds >= $hour && $seconds < $day):
            $hours = floor($seconds /$hour);
            if ($hours > 1) 
                $time_ago = "$hours hours ago";
            else
                $time_ago = "1 hour ago";
            break;


        case ($seconds >= $day && $seconds < $month):
            $days = floor($seconds /$day);
            if ($days > 1) 
                $time_ago = "$days days ago";
            else
                $time_ago = "1 day ago";
            break;


        case ($seconds >= $month && $seconds < $year):
            $months = floor($seconds /$month);
            if ($months > 1) 
                $time_ago = "$months months ago";
            else
                $time_ago = "1 month ago";
            break;

        case ($seconds >= $year ):
            $years = floor($seconds /$year);
            if ($years > 1) 
                $time_ago = "$years years ago";
            else
                $time_ago = "1 year ago";
            break;


        default:
            break;
        }
    return $time_ago;
    }