-1

I am trying to display user friendly date formatting such as "1 hour and 15 minutes", "4 days and 8 hours" to the user. However my script is displaying 0 hours as 23 for some reason.

 $date = '2014-01-15 15:00' # PAST DATE
    $now        = Date("Y-m-d H:m:s");
            $seconds    = strtotime($now) - strtotime($date);

            $days    = floor($seconds / 86400);
            $hours   = floor(($seconds - ($days * 86400)) / 3600);
            $minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
            $seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));

            if($days > 0)
            {
                if($days == 1)
                {
                    return $days . ' dag ' . $hours . ' timmar';
                } else {
                    return $days . ' dagar ' . $hours . ' timmar';
                }
            }

            if(($hours < 24) AND ($hours > 0))
            {
                return $hours . ' timmar';
            }

            if($minutes < 60)
            {
                return $minutes . ' minuter';
            }

Can anyone see what is causing this? Am I doing it the correct way? Note that $date is user supplied in the past.

user3218338
  • 652
  • 1
  • 8
  • 20

2 Answers2

3

There are much easier ways to do this:

$past = new DateTime('2014-01-15 15:00');
$now = new DateTime();
$interval = $now->diff($past);
echo $interval->format('%y years, %m months, %d days, 
                                %h hours, %i minutes, %S seconds');

An obvious improvement is to use now show periods of time that have zero values (i.e. 0 days):

$elapsed = $interval->format('%y years, %m months, %d days, 
                              %h hours, %i minutes');
$elapsed = str_replace(array('0 years,', ' 0 months,', ' 0 days,', 
                            ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ', 
                             ' 1 hours, ', ' 1 minutes'), array('1 year, ', 
                             '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), 
                       $elapsed);
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Ok, that narrowed it down a bit. However I only wish to display days if there actually are any. Same with hours. Added some if statements and I am done! – user3218338 Feb 25 '14 at 13:59
  • See my updated answer. It *might* need improvement but should get you started for sure. – John Conde Feb 25 '14 at 14:01
0

Use something like this:

            $date = '2014-01-15 15:00' # PAST DATE
            now        = Date("Y-m-d H:m:s");
            $t    = strtotime($now) - strtotime($date);    

            $time = date('g:iA M dS', $t );
            $diff = time() - $t;
            if ( $diff < 60 )
            {
                return "a few seconds ago";
            }
            elseif ( $diff < 3600 )
            {
                return "about ".( int ) ($diff/60) ." mins ago";
            }
            elseif ( $diff < 86400 )
            {
                if ( ( int ) ($diff/3600) == 1 )
                {
                    return "about an hour ago";
                }
                else
                {
                    return "about ".( int ) ($diff/3600) ." hours ago";
                }
            }
            else if( $diff < 172800 )
            {
                return "about a day ago";
            }
            elseif ( $diff > 172800 )
            {
                return "$time"; 
            }
Chethan N
  • 1,110
  • 1
  • 9
  • 23