0

I have been trying to get this code working properly - basically I would like to display the time elapsed since and article was posted by subtracting it from the current time. I have almost got it, I found this code below and all works well except for the minutes and seconds. I can figure out why its not showing the seconds or minutes if applicable. Just to clear each article shows the applicable elapsed time so if < 60s it should show howmany seconds, > then should show minutes etc

my code is

    <?php  
    $today = time();
    $post = $item->created;
             $createdday= strtotime($post); //convert $post to unix timestamp
             $datediff = abs($today - $createdday);  
             $difftext="";  
             $years = floor($datediff / (365*60*60*24));  
             $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
             $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
             $hours= floor($datediff/3600);  
             $minutes= floor($datediff/60);  
             $seconds= floor($datediff);  
             //year checker  
             if($difftext=="")  
             {  
               if($years>1)  
                $difftext=$years." years ago";  
               elseif($years==1)  
                $difftext=$years." year ago";  
             }  
             //month checker  
             if($difftext=="")  
             {  
                if($months>1)  
                $difftext=$months." months ago";  
                elseif($months==1)  
                $difftext=$months." month ago";  
             }  
             //month checker  
             if($difftext=="")  
             {  
                if($days>1)  
                $difftext=$days." days ago";  
                elseif($days==1)  
                $difftext=$days." day ago";  
             }  
             //hour checker  
             if($difftext=="")  
             {  
                if($hours>1)  
                $difftext=$hours." hours ago";  
                elseif($hours==1)  
                $difftext=$hours." hour ago";  
             }  
             //minutes checker  
             if($difftext=="")  
             {  
                if($minutes>1)  
                $difftext=$minutes." minutes ago";  
                elseif($minutes==1)  
                $difftext=$minutes." minute ago";  
             }  
             //seconds checker  
             if($difftext=="")  
             {  
                if($seconds>1)  
                $difftext=$seconds." seconds ago";  
                elseif($seconds==1)  
                $difftext=$seconds." second ago";  
             }  
             echo " <span class=timediff> | ".$difftext . "</span>";  ?> 

and my example is here http://www.landnsand.co.za/dev/test/ the latest reading module below the slides.

Any suggestions or advise as to why the seconds and minutes may not be working would be greatly appreciated! I have searched high and low and have tried alot of options but must be missing something.

With Thanks

ZADorkMan
  • 301
  • 4
  • 19
  • So you found the code below. Can you explain it or did you just copy/paste it? – Realitätsverlust Jan 17 '14 at 10:54
  • 1
    Yes I found it. I can explain it yes but cannot seem to figure out why the seconds and minutes options don't work when time elapsed is less than 60 seconds and less than 60 minutes. – ZADorkMan Jan 17 '14 at 10:56
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/a/18602474/67332) – Glavić Jan 17 '14 at 10:57
  • Can you output variable `$post` ? – Glavić Jan 17 '14 at 12:41
  • yes I can - it is the articles post date not formatted into unix string – ZADorkMan Jan 20 '14 at 08:18
  • I got it working - it did indeed have to do with the timezone - I added date_default_timezone_set("UTC"); and it seems to work a dream now ;) thanks everyone! – ZADorkMan Jan 20 '14 at 09:59

2 Answers2

0

Use example :

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);

Output :

4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

Link to the function.

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Hi I see your code has allowance for the plural 's' however if you see in my code and example website that the day/days plural works but when it tries to use hours, min or seconds it just fails. The hours time is in inaccurate by about 2 hours and the minutes and seconds dont work at all – ZADorkMan Jan 17 '14 at 12:24
  • as a matter of fact on the site the left item i just posted a few seconds ago and it says its 2 hours old - thats the problem I am facing now - the year, month and days work fine its just the hours minutes and seconds which are misbehaving... :) – ZADorkMan Jan 17 '14 at 12:32
  • @user3206244: that seems to be timezone issue. Have you tried my function? Does it return same? – Glavić Jan 17 '14 at 12:41
  • thanks Glavić I tried your function and I can see the seconds on each page refresh however I don't see how to integrate that into my selection. Will give it another go. :) – ZADorkMan Jan 20 '14 at 06:53
  • if anybody is using Joomla 2.5 and could possibly test my code and let us know if it gives the same result for new items that would be awesome! (showing time elapsed from only 2 hours ago even if it was just posted, so even if i just publish something it only shows published '2 hours ago' nothing earlier) Apologies! I am still learning the finer details of php :) – ZADorkMan Jan 20 '14 at 07:59
-1
<?php
$dt = new DateTime();
$dt->setTimestamp(1389956508); // timestamp of creation (example)
$dt2 = new DateTime();
$dt2->setTimestamp(time()); // timestamp now
$interval = $dt->diff($dt2, true);
echo $interval->format('%i minutes ago');
?>

Output (for example):

2 minutes ago

That's just an example. So, it is very easy to use it in this way.

Yannici
  • 736
  • 5
  • 17
  • Thanks very much to you both! I see how your functions work - however in my code there is a condition which states if the time elapsed is equals to 1 of its units ie year and years, month and months that it displays the relevent time with an plural s or not. – ZADorkMan Jan 17 '14 at 11:24
  • @user3206244 I have a very simple class [here](https://github.com/vascowhite/TimeAgo/blob/master/TimeAgo.php) that does what you need, including using singular or plural as appropriate. – vascowhite Jan 19 '14 at 12:22