...

View Full Version : Help with time ago



Greenbox
06-17-2010, 01:55 PM
hi, im trying to create something facbook like when you post a topic in the forum it shows for example 1 minute ago, but when i use this code it shows up -21244 secs ago, anyone who have an idea why it dosent show 1 second ago?


time ago class called develop_php_libary.php


<?php
// CLASS FOR CONVERTING TIME TO AGO
class convertToAgo {

function convert_datetime($str) {

list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}

function makeAgo($timestamp){

$difference = time() - $timestamp;
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}

} // END CLASS
?>



rightforum.php


<?php
session_start();
include_once "includes/db_connect.php";
include_once"includes/functions.php";
include"includes/smile.php";
include_once("classes/develop_php_library.php"); // Include the class library


$query=mysql_query("$query");
$num=mysql_num_rows($query);
while($right=mysql_fetch_object($query)){


if ($fetch->userlevel > "0"){

$timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
// Query your database here and get timestamp
$ts = "$right->made";
$convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
$when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time

$forumquote = mysql_query("SELECT * FROM users WHERE username = '$right->username'");
$fq = mysql_fetch_object($forumquote);
echo "
<table class='GridRow'>
<tr class=GridHeader>
<td>
<a href='profile.php?viewuser=$right->username'>$right->username</a>
[$when]
<a href='rightforum.php?clean=$right->id&forum=$forum&viewtopic=$viewtopic'>[Delete]</a>
</td>

<tr><td width=433>";

echo replace(stripslashes($right->text));

if($fq->forumquote == "") {
echo "</td></tr></table><br>
<p>";
}
else {
echo "</td></tr><tr></tr></table><br>
<p>"; }
}elseif ($userlevel == 0){

$timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
// Query your database here and get timestamp
$ts = "$right->made";
$convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
$when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time


$forumquote = mysql_query("SELECT * FROM users WHERE username = '$right->username'");
$fq = mysql_fetch_object($forumquote);
echo "
<table Class='GridRow'>
<tr Class='GridHeader'>
<td>

<a href='profile.php?viewuser=$right->username' target=middle>$right->username</a>
[$when]
</td>
</tr>
<tr>
<td width=433>
</b>";

echo replace(stripslashes($right->text));


if($fq->forumquote == "") {
echo "</td></tr></table><br><p>";
}
else {
echo "</td></tr></table><br>
<p>"; }
}



}
?>


yeah, i know it is a bit messy, im new to php :)

Thanks in advance ;)

Fou-Lu
06-17-2010, 03:39 PM
Looks ok to me, so it must be the data.
Except for this: for($j = 0; $difference >= $lengths[$j]; $j++). The highest you can go is a decade, what happens if its greater than this? Chances are it won't be, but you should change this:


$iLength = count($periods); // or lengths, it doesn't matter since both should be the same length
for($j = 0; $difference >= $lengths[$j] && $j < $iLength; $j++)


So, lets see what's in timestamp:


function makeAgo($timestamp){

$difference = time() - $timestamp;
printf("\$timestamp: %d, \$difference: %d\n", $timestamp, $difference);
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}


What does that show for your input data.

Greenbox
06-17-2010, 10:22 PM
it shows me 6 hours when i post :P

Fou-Lu
06-17-2010, 10:53 PM
it shows me 6 hours when i post :P

So its working properly for you now?

Greenbox
06-18-2010, 12:36 AM
it dosent show, -232342 but it shows 6 hours when i post it, it should say 1 second ago :P

Zoic
06-18-2010, 12:48 AM
You can do this with a jQuery plugin. I use it myself actually. :)

http://timeago.yarp.com/

Its very easy to implement.

Fou-Lu
06-18-2010, 01:16 AM
it dosent show, -232342 but it shows 6 hours when i post it, it should say 1 second ago :P

What does the actual time say for it? Being an even hour, I'm leading towards a timezone offset.

Greenbox
06-18-2010, 01:00 PM
The actual time:
2010-06-17 09:21:16 -> 21 hrs ago

im not sure if thats what you ment, but it shows 6 hours to much for some reason, it might be the timezone..

Fou-Lu
06-18-2010, 03:41 PM
If you create a brand new one, and check the time for it, does it list the proper time for it or does it show it as -6 hours instead of the 0-whatever seconds ago?
If it does, its a timezone issue. This can be corrected by setting the proper timezone for what you would like to use.
I'm fairly certain as well that using strtotime will parse both parts (date and time sections) into a proper timestamp. Since your format matches a valid input format (ISO-8601 / yyyy-mm-dd), this can be used in place of the convert_datetime method if you like.

DJCMBear
06-19-2010, 02:12 PM
Heres what I created for one of my recent systems.
To use this function you just write this postTime('13-06-2010 03:19AM');


<?php
function postTime($time1)
{
$time1 = strtotime($time1);
$time2 = time();
$diff = array(
'years' => 0, 'months' => 0, 'weeks' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0
);
foreach(array('years','months','weeks','days','hours','minutes','seconds') as $unit)
{
while(true)
{
$next = strtotime("+1 $unit", $time1);
if($next < $time2)
{
$time1 = $next;
$diff[$unit]++;
}
else
{
break;
}
}
}
if($diff['years'] > 0)
{
if($diff['months'] > 0 || $diff['weeks'] > 0 || $diff['days'] > 0 ||
$diff['hours'] > 0 || $diff['minutes'] > 0 || $diff['seconds'] > 0)
{
$time = ($diff['years'] == '1')?"over {$diff['years']} year":"over {$diff['years']} years";
}
else
{
$time = ($diff['years'] == '1')?"{$diff['years']} year":"{$diff['years']} years";
}
}
elseif($diff['months'] > 0)
{
if($diff['weeks'] > 0 || $diff['days'] > 0 || $diff['hours'] > 0 ||
$diff['minutes'] > 0 || $diff['seconds'] > 0)
{
$time = ($diff['months'] == '1')?"over {$diff['months']} month":"over {$diff['months']} months";
}
else
{
$time = ($diff['months'] == '1')?"{$diff['months']} month":"{$diff['months']} months";
}
}
elseif($diff['weeks'] > 0)
{
if($diff['days'] > 0 || $diff['hours'] > 0 || $diff['minutes'] > 0 || $diff['seconds'] > 0)
{
$time = ($diff['weeks'] == '1')?"over {$diff['weeks']} week":"over {$diff['weeks']} weeks";
}
else
{
$time = ($diff['weeks'] == '1')?"{$diff['weeks']} week":"{$diff['weeks']} weeks";
}
}
elseif($diff['days'] > 0)
{
if($diff['hours'] > 0 || $diff['minutes'] > 0 || $diff['seconds'] > 0)
{
$time = ($diff['days'] == '1')?"over {$diff['days']} day":"over {$diff['days']} days";
}
else
{
$time = ($diff['days'] == '1')?"{$diff['days']} day":"{$diff['days']} days";
}
}
elseif($diff['hours'] > 0)
{
if($diff['minutes'] > 0 || $diff['seconds'] > 0)
{
$time = ($diff['hours'] == '1')?"over {$diff['hours']} hour":"over {$diff['hours']} hours";
}
else
{
$time = ($diff['hours'] == '1')?"{$diff['hours']} hour":"{$diff['hours']} hours";
}
}
elseif($diff['minutes'] > 0)
{
if($diff['seconds'] > 0)
{
$time = ($diff['minutes'] == '1')?"over {$diff['minutes']} minute":"over {$diff['minutes']} minutes";
}
else
{
$time = ($diff['minutes'] == '1')?"{$diff['minutes']} minute":"{$diff['minutes']} minutes";
}
}
elseif($diff['seconds'] > 0)
{
$time = ($diff['seconds'] == '1')?"{$diff['seconds']} second":"{$diff['seconds']} seconds";
}
if(!empty($time))
{
return "{$time} ago";
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum