xx time ago function doesn't work - php

I am trying to display "{number} {unit-of-time} ago" in my chat. However, when I'm expecting something like "2 minutes ago", I see "45 years" instead.
Nobody has an answer?
Here is the script:
<?php
$con = mysql_connect("localhost", "user", "pword");
mysql_select_db('chat', $con);
$result1 = mysql_query("SELECT * FROM logs ORDER BY id ASC");
$tm = mysql_query("SELECT timestamp FROM logs ORDER BY id ASC");
function _ago($tm, $rcs = 0)
{
$cur_tm = time();
$dif = $cur_tm - $tm;
$pds = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
$lngh = array(1, 60, 3600, 86400, 604800, 2630880, 31570560);
for ($v = sizeof($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--) ;
if ($v < 0) $v = 0;
$_tm = $cur_tm - ($dif % $lngh[$v]);
$no = floor($no);
if ($no <> 1) $pds[$v] .= 's';
$x = sprintf("%d %s ", $no, $pds[$v]);
if (($rcs == 1) && ($v >= 1) && (($cur_tm - $_tm) > 0)) $x .= time_ago($_tm);
return $x;
}
$timeagochat = _ago($tm, $rcs = 0);
while ($extract = mysql_fetch_array($result1)) {
echo "<p class='show_message'><span class='uname'>" . $extract['username'] . "</span> <span class='time'>" . $timeagochat . " </span><br><span class='msg'>" . $extract['msg'] . "</span></p><br>";
}
?>
What can I do to let this function work?

You have to fetch the date:
$tm = mysql_query("SELECT timestamp AS t FROM logs ORDER by id ASC")->fetch_assoc()['t']; //obviously, check the size before you fetch
You probably want to use strtotime() so for instance:
$dif = $cur_tm-strtotime($tm);
The $dif will be in milliseconds.
What are you doing with the loops and the arrays? Too confusing.
5. WHY DO PEOPLE KEEP USING mysql_!!!

Related

Speeding up PHP script

I'm working on a calendar for my office. Every people has his own column and there is a line for every day.
There are some periodic date, where, for example, given people have to be working on the week-end. But most of the dates are coming from a MySQL database.
So it does a double loop (people vs dates) in which every dates have to be check for the person, what kind of occupation he has on this day.
Is there a way to optimize this script, because online it take at least 2-3 seconds (only 0.03 seconds according to PHP, but I don't feel like it's correct) and more than 8 seconds (again according to PHP) on our network! And this is just for 5 months, we'd like to have it for the whole year.
You can find a test version here (just to see the HTML and CSS): http://mybees.ch/for/tableau.php
And here is the PHP in it:
//Creating the line for the collaborators
$ids;
$ma_count=0;
$ma_name_query = mysqli_query($bdi,"SELECT DISTINCT m.id, m.name, m.vorname, m.grup FROM ma m, ma_pos mp WHERE m.id = mp.maid AND m.grup != 14 ORDER BY m.grup, mp.posid, m.name, m.vorname");
while($ma_name = mysqli_fetch_assoc($ma_name_query)) {
echo '<div class="cell name">'.$ma_name['name'].' '.$ma_name['vorname'].'</div>';
$ids[$ma_count] = $ma_name['id'];
$grup[$ma_count] = $ma_name['grup'];
$ma_count++;
}
// Check if special group day
$firstGroup = 1;
$firstDate = strtotime("$year-01-01 00:00 GMT");
$picket = array();
if (date("N", $firstDate) == 2)// Tuesday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 3)// Wednesday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 5)// Friday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 6)// Saturday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 7)// Sunday
$firstDate -= 3 * 24 * 3600;
for ($date = $firstDate; $date <= strtotime("$year-12-31 00:00 GMT"); $date += 24 * 3600) {
$weekNb = date("W",$date);
$weekDay = date("N",$date); // Monday = 1, Sunday = 7
if ($weekDay < 4)
$group = $weekNb % 4 - 1 + $firstGroup;
else
$group = $weekNb % 4 - 2 + $firstGroup;
if ($group == 0)
$group = 4;
if ($group == -1)
$group = 3;
$picket[$date] = $group;
}
$groupColor = ["yellow", "blue", "red", "green"];
function isPicket($date, $grup) {
global $picket, $groupColor;
//global $picket, $groupColor;
if ($grup < 5) {
if ($picket[$date] == $grup)
return " style='background-color: ".$groupColor[$picket[$date]-1]."'";
}
}
$today_stp = time() - time() % (24 * 3600) - 11 * 24 * 3600;
$frei_query_text = "SELECT id_ma, date1, date2, free.id as type, moment, remark, location FROM frei, free WHERE free.id = frei.type AND date1 BETWEEN CAST('$date1' AS DATE) AND CAST('$date2' AS DATE)";
$frei_query = mysqli_query($bdi, $frei_query_text);
$free = array();
while($frei = mysqli_fetch_assoc($frei_query)) {
$free[] = $frei;
}
// Filling the lines
for ($date = strtotime($date1); $date <= strtotime($date2); $date += 24 * 3600) {
$today = ($date == $today_stp) ? ' id="today"':'';
echo "<div class='clear'$today>";
$class = (date('N', $date) < 6) ? 'week' : 'weekend';
echo "<div class='date $class line'>".date("D, d.m.", $date)."</div>";
for ($i = 0; $i < $ma_count; $i++) {
echo "<div class='cell line $class'".isPicket($date,$grup[$i]).">
<div class='small-cell".isColor($ids[$i], $date, 0)."' id='divUp-".$ids[$i]."-$date'> </div>
<div class='small-cell".isColor($ids[$i], $date, 1)."' id='divDown-".$ids[$i]."-$date'> </div>
</div>";
}
echo '</div>';
}
function isColor($id_ma, $date, $moment) {
global $free;
for ($i = 0; $i < count($free); $i++) {
if ($id_ma == $free[$i]['id_ma']) {
if ($date >= strtotime($free[$i]['date1']) && $date <= strtotime($free[$i]['date2'])) {
if ($free[$i]['moment'] == $moment || $free[$i]['moment'] == 2) {
$type = $free[$i]['type'];
$style = "";
if ($type > 1 && $type < 5)
$style = " urlaub";
if ($type > 4 && $type < 8)
$style = " frei";
if ($type > 7 && $type < 11)
$style = " ferien";
if (($type > 22 && $type < 34) || ($type > 37 && $type < 48))
$style = " kurse";
if ($type > 10 && $type < 17)
$style = " krank";
return " $style' title='".$free[$i]['remark'];
}
}
}
}
}
Thank you very much for your help

How to display time_ago in another function

This function is to display what inside my table but i can't figure out how to dislay time_ago function where it should display seconds or hours or weeks just want to display seconds hour or week instead of month day and year but i get an error Call to undefined function time_ago() i don't know what i did wrong but i just can figure it out what went wrong with my code. got error on line 505 which is this ---> ' . time_ago($row['lastupdate']) . '
public static function getTicketByUserID(){
$mysqli = dbConnect();
$sql = "select
t.id as ticketid,
t.clientid,
t.comments,
t.assigneduser,
t.subject,
t.user,
t.lastupdate,
t.opendate,
(select c.department_name from department c where c.department_id = t.categoryid) as category,
(select ts.status
from ticketstatus ts
where ts.ticketid = t.id
and ts.statusdate = (select max(ts2.statusdate)
from ticketstatus ts2
where ts.ticketid = ts2.ticketid)) as status
from tickets t
where t.user = '". $_SESSION['name'] ."'
and (select ts.status
from ticketstatus ts
where ts.ticketid = t.id
and ts.statusdate = (select max(ts2.statusdate)
from ticketstatus ts2
where ts.ticketid = ts2.ticketid)) <> 'Closed'
order by t.opendate desc";
$result = $mysqli->query($sql);
echo '<table class="table table-hover table-striped table-responsive"><th>User</th><th>Subject</th><th>Group</th><th>Created</th><th>Last update</th><th>Status</th>';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row['status'] == 'Closed') { $class = "btn btn-danger";}
elseif ($row['status'] == 'Open') { $class = "btn btn-success";}
elseif ($row['status'] == 'Waiting on Client') {$class = "btn btn-info";}
else { $class = "btn btn-warning"; }
echo '<tr class="table_row">
<td>' .$row['user'] .'</td>
<td>' .$row['subject'] .'</td>
<td>' . $row['category'] . '</td>
<td>' . date("F j, Y, g:i a", strtotime($row['opendate'])) . '</td>
<td>' . time_ago($row['lastupdate']) . '</td>
<td><form method="POST">
<input name="ticketId" value="' . $row['ticketid'] . '" type="text" hidden />
<button type="submit" class="'.$class .'">'.$row['status'] .'</button></form>
</td>
</tr>';
}
} else {
echo '</table>';
$conn->close();
}
This is my function for time_ago get this error Call to undefined function time_ago() what did i do wrong?
function time_ago($ts)
{
$mins = floor((gmtime() - $ts)/60);
$hours = floor($mins/60);
$mins -= $hours*60;
$days = floor($hours/24);
$hours -= $days*24;
$weeks = floor($days/7);
$days -= $weeks*7;
if ($weeks == 1)
$w = "week";
elseif ($weeks > 1 && $weeks < 5)
$w = "weeks";
elseif ($weeks > 4)
$w = "weeks";
if ($weeks >= 1){
$we = "".$weeks." ".$w."";
}
if ($days == 1)
$d = "day";
elseif ($days > 1 && $days < 5)
$d = "day";
elseif ($days > 4)
$d = "days";
if ($days >= 1 ){
$da = "".$days." ".$d."";
}
if ($hours == 1)
$h = "hour";
elseif ($hours > 1 && $hours < 5)
$h = "hour";
elseif ($hours > 4)
$h = "hours";
if ($hours >= 1){
$ho = "".$hours." ".$h."";
}
if ($mins > 10 && $mins < 15)
$m = "minute";
elseif (($mins%10) == 1)
$m = "minute";
elseif (($mins%10) > 1 && ($mins%10) < 5)
$m = "minutes";
elseif (($mins%10) > 4)
$m = "minute";
if ($mins >= 1){
$mi = "".$mins." ".$m."";
}else{
$mi = "less than a minute";
}
if ($weeks || $days || $hours || $mins)
return $we." ".$da." ".$ho." ".$mi;
}

Display when user was last online using PHP

I'm needing help with trying to display the users last online time.
My aim is to make it so that if the users status is 'online', display last seen just now, else, display the last time they were seen. Or if it's better, just display their last online time including seconds.
My code works but for some reason, when the user is offline, it displays user last online 1 hour ago if it I logged out less than 10 seconds ago.
My code is below
//Our default online script to get the last active time period from our lastactive time
function time_since($since)
{
global $con;
global $user_infos;
foreach($user_infos as $user)
{
$last_active = $user[7];
$user_status = $user[6];
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
array(1 , 'second')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++)
{
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0)
{
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
$check = $user_status == 'online' ? 'last online just now' : 'last online '.$print;
return $check;
}
}
// I made to make it firstly, update the users lastactive time for each page they go on by calling the function user_online_check() in the header (which is called for every page)
// and secondly, make it return a green image if the client is online and a red image if the client is offline
function user_online_check()
{
global $con;
global $user_infos;
global $time;
foreach($user_infos as $user)
{
$username = $_SESSION["user_i"];
$now = date("Y-m-d H:i:s");
//mysqli_query($con, "UPDATE users SET lastactive = NOW() WHERE username = '$username'");
$last_active = $user[7];
$user_status = $user[6];
mysqli_query($con, "UPDATE users SET status = 'offline' WHERE TIMESTAMPDIFF(MINUTE, lastactive, NOW()) > 1 AND username = '$username' LIMIT 1");
mysqli_query($con, "UPDATE users SET status = 'online' WHERE TIMESTAMPDIFF(MINUTE, lastactive, NOW()) < 1 AND username = '$username' LIMIT 1");
if($user_status == 'online')
{
echo '<center><img src="images/user/bullet_green.ico" style="width: 20px; height: 20px;" title="'.time_since(time() - strtotime($last_active)).'"></center>';
}
else if($user_status == 'offline')
{
echo '<center><img src="images/user/bullet_red.ico" style="width: 20px; height: 20px;" title="'.time_since(time() - strtotime($last_active)).'"></center>';
}
}
}
try this
$seen = floor((time("now")-$row['time'])/60);
$more = false;
if($seen > 60) {
$more = true;
$hours = floor($seen/60);
$minutes = $seen-($hours*60);
if(($seen > 24) && ($more == true)) {
$days = floor(($seen/60)/24);
$hours = floor($seen/60)-($days*24);
}
if($minutes == 1) {
$minute = ' minute ';
} else {
$minute = ' minutes ';
}
if($hours == 1) {
$hour = ' hour ';
} else {
$hour = ' hours ';
}
if($days == 1) {
$day = ' day ';
} else {
$day = ' days ';
}
if($days > 0) {
$seen = $days . $day . $hours . $hour . $minutes . $minute . 'ago';
} else {
$seen = $hours . $hour . $minutes . $minute . 'ago';
}
} else {
if($seen == 1) {
$minute = ' minute ';
} else {
$minute = ' minutes ';
}
$seen = $seen . $minute . 'ago';
}

Show x time ago if time is less than 24 hour ago

I use Codeigniter and it has the timespan() function that returns the time as 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes.
What I'd like to do is only show the time formatted in x hours ago if the time is within the last 24 hours, otherwise just show a normal datetime.
I feel like there's got to be a function already made to do this but I haven't had any luck finding it.
This is the timespan function included with Codeigniter, how can I alter it?
/**
* Timespan
*
* Returns a span of seconds in this format:
* 10 days 14 hours 36 minutes 47 seconds
*
* #access public
* #param integer a number of seconds
* #param integer Unix timestamp
* #return integer
*/
if ( ! function_exists('timespan'))
{
function timespan($seconds = 1, $time = '')
{
$CI =& get_instance();
$CI->lang->load('date');
if ( ! is_numeric($seconds))
{
$seconds = 1;
}
if ( ! is_numeric($time))
{
$time = time();
}
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
$seconds = $time - $seconds;
}
$str = '';
$years = floor($seconds / 31536000);
if ($years > 0)
{
$str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
}
$seconds -= $years * 31536000;
$months = floor($seconds / 2628000);
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
$str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
}
$seconds -= $months * 2628000;
}
$weeks = floor($seconds / 604800);
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
if ($weeks > 0)
{
$str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
}
$seconds -= $weeks * 604800;
}
$days = floor($seconds / 86400);
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
}
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
}
$seconds -= $hours * 3600;
}
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
}
return substr(trim($str), 0, -1);
}
}
This function will accept a string, a numeric (unix) timestamp, or a DateTime object. It also accepts jQuery.now(). Time may be in the future or past.
function time_ago($time=false, $just_now=false) {
if ($time instanceOf DateTime)
$time = $time->getTimestamp();
elseif (is_numeric($time))
$time = date('m/d/y h:i A', $time);
if (strtotime($time) === false)
$time = date('m/d/y h:i A', time());
$interval = date_create($time)->diff(date_create('now'));
$adjective = strtotime($time) > time() ? 'from now' : 'ago';
return (
$interval->days > 0 ?
$time : (
$interval->h < 1 && $interval->i < 1 && $just_now ?
'just now' :
(
$interval->h > 1 ?
$interval->h.' hour'.(
$interval->h > 1 ?
's' :
''
).' ago' :
$interval->i.' minutes'.' '.$adjective
)
)
);
}
echo time_ago('8/22/2012 5:00 PM'); // 3 hours ago
echo time_ago('8/21/2012 5:00 PM'); // 8/21/2012 5:00 PM
echo time_ago(time()); // 0 hours ago
echo time_ago(time(), true); // just now
echo time_ago(strtotime('5 days ago')); // 08/17/12 08:18 PM
echo time_ago(strtotime('5 hours ago')); // 5 hours ago
echo time_ago(strtotime('5 minutes ago')); // 5 minutes ago
echo time_ago(strtotime('+5 minutes')); // 5 minutes from now
echo time_ago('jQuery.now()', true); // just now
echo time_ago('sweet explosions, bro!', true); // just now
Documentation
date - http://php.net/manual/en/function.date.php
DateTime object - http://www.php.net/manual/en/book.datetime.php
DateInterval object - http://www.php.net/manual/en/class.dateinterval.php
is_numeric - http://php.net/manual/en/function.is-numeric.php
if( time() - $yourTime <= 86400 ) { // 86400 seconds in a day
echo timespan($yourTime);
} else {
echo date('m/d/Y \a\t H:i:s', $yourTime);
}
Don't rely on a framework, especially a bad one, for everything!
Fiddle with $format to suit your needs. Will accept almost anything as input.
<?php
/**
* RelativeTime - pretty printed
* #author Dejan Marjanovic
*/
class Site5_RelativeTime
{
private $interval = '';
public function __construct()
{
call_user_func_array(array($this, 'calculate'), func_get_args());
}
public function calculate($start, $end = NULL)
{
if ( empty($start))
return false;
if (empty($end))
$end = time();
if ( ! is_numeric($start))
$start = strtotime($start);
if ( ! is_numeric($end))
$end = strtotime($end);
if($start > $end)
$future = TRUE;
$start = '#' . $start;
$end = '#' . $end;
if ( ! ($start instanceof DateTime))
$start = new DateTime($start);
if ($end === null)
$end = new DateTime();
if ( ! ($end instanceof DateTime))
$end = new DateTime($end);
$interval = $end->diff($start);
$get_plural = function($int, $str)
{
return $int > 1? $str.'s': $str;
};
$format = array();
if ($interval->y !== 0)
$format[] = "%y " . $get_plural($interval->y, "year");
if ($interval->m !== 0)
$format[] = "%m " . $get_plural($interval->m, "month");
if ($interval->d !== 0)
$format[] = "%d " . $get_plural($interval->d, "day");
if ($interval->h !== 0)
$format[] = "%h " . $get_plural($interval->h, "hour");
if ($interval->i !== 0)
$format[] = "%i " . $get_plural($interval->i, "minute");
if ($interval->s !== 0)
{
if ( ! count($format))
{
$this->interval = "less than a minute";
return;
}
else
{
$format[] = "%s " . $get_plural($interval->s, "second");
}
}
if (count($format) > 1)
{
$format = array_shift($format) . " and " . array_shift($format);
}
else
{
$format = array_pop($format);
}
$tense = ($future === TRUE)? 'from now': 'ago';
$this->interval = $interval->format($format) . ' ' . $tense;
}
public function __toString()
{
return $this->interval;
}
}

codeigniter timespan function

Hello i have now search the hole web and found a lot but i just dont know how to make it to work so now im asking here for help
i want to do so then a person create a comment it should said "created 1 sec. ago" and then 1 min and 1 hour and like that :)
can some one help me with that ?
thanks
This is basically human readable format, and can be completed by mathematical checks to check the distance of times, working snippet below:
function RelativeTime($timestamp)
{
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
if ($difference > 0)
{
$ending = "ago";
}
else
{
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
return $difference . $periods[$j] . $ending;
}
This will do future timestamps such as 12 days to go aswell as timestamps such as 12 days ago
Hope this helps.
Original Source: http://blog.evandavey.com/2008/04/php-date-in-human-readable-form-facebook-style.html
I think this is exactly what you want. When you using the function set $deep parameter to 1.
function timespan($seconds = 1, $time = '', $deep = NULL)
{
$CI = & get_instance();
$CI->lang->load('date');
$current_deep = 0;
if (!is_numeric($seconds))
{
$seconds = 1;
}
if (!is_numeric($time))
{
$time = time();
}
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
$seconds = $time - $seconds;
}
$str = '';
$years = floor($seconds / 31536000);
if ($years > 0)
{
$str .= $years . ' ' . $CI->lang->line((($years > 1) ? 'date_years' : 'date_year')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $years * 31536000;
$months = floor($seconds / 2628000);
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
$str .= $months . ' ' . $CI->lang->line((($months > 1) ? 'date_months' : 'date_month')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $months * 2628000;
}
$weeks = floor($seconds / 604800);
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
if ($weeks > 0)
{
$str .= $weeks . ' ' . $CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $weeks * 604800;
}
$days = floor($seconds / 86400);
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str .= $days . ' ' . $CI->lang->line((($days > 1) ? 'date_days' : 'date_day')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str .= $hours . ' ' . $CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $hours * 3600;
}
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str .= $minutes . ' ' . $CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds . ' ' . $CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')) . ', ';
}
return substr(trim($str), 0, -1);
}
Source
Assuming you have the difference $now - $creation_time in seconds, a way to do it is to divide it by X seconds (1 minute = 60, 1 hour = 3600, 1 day = 86400) starting with the largest number to see how many of those units fit in your creation time, then use its remainder to try and fit the smaller units in.
$diffSeconds = time() - $creation_time ;
$numDays = $diffSeconds / 86400 ;
$remainderDaySeconds = $diffSeconds % 86400 ;
$numHours = $remainderDaySeconds / 3600 ;
$remainderSeconds = $remainderDaySeconds % 3600 ;
The modulo operator % will give you the remainder of a division. This way, if a post was created less than a day ago then $numDays is 0 and $remainderDaySeconds is $diffSeconds, so you can check and print out accordingly.
Edit I got curious and looked in SO, turns out there are quite a few questions expanding on this. Linking some:
Calculate relative time in C#
calculating and showing a date as 'secs ago', 'mins ago', 'hours ago' etc which points to http://www.php.net/manual/en/function.time.php#89415

Categories