How to fix Timeago errors - php

I have a time ago function in php which i use to return if the user is online or when the user was last seen.
function tj_online_last($ptime) {
$estimate_time = time() - strtotime($ptime);
// if time diff is less than 1 minute then user is online
if ($estimate_time < 60) {
return 'online';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second',
);
foreach($condition as $secs => $str) {
$d = $estimate_time / $secs;
if ($d >= 1) {
$r = round($d);
return $r.' '.$str.($r > 1 ? 's' : '').' ago';
}
}
}
Now the problem here when the user has not been logged into his account for the first time the time value is 'NULL'. I would like to return 'never' instead of the annoying '48 years ago' because I am not able to handle the error.

Try this...
When user first time login then it's last entry is null so you need to check condition if last date is empty then return 'never';
function tj_online_last($ptime) {
if(strtotime($ptime) <= 0){
return 'never';
}
$estimate_time = time() - strtotime($ptime);
// if time diff is less than 1 minute then user is online
if ($estimate_time < 60) {
return 'online';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second',
);
foreach($condition as $secs => $str) {
$d = $estimate_time / $secs;
if ($d >= 1) {
$r = round($d);
return $r.' '.$str.($r > 1 ? 's' : '').' ago';
}
}
}

Related

How do I get a time difference estimate from right now in PHP. Similar to moment.js fromNow() function

I normally use moment.js fromNow() to get a time difference estimate from the current time. (Will return things like a "a few seconds ago", "3 days ago", etc.)
I know that PHP's date_diff() function will return the exact time difference, but I need just an estimate of sorts.
try this
function get_time_ago($time)
{
$time_difference = time() - strtotime($time);
if( $time_difference < 1 ) { return 'less than 1 second ago'; }
$condition = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $condition as $secs => $str )
{
$d = $time_difference / $secs;
if( $d >= 1 )
{
$t = round( $d );
return 'About ' . $t . ' ' . $str . ( $t > 1 ? 's' : '' ) . ' ago';
}
}
}
Example
echo get_time_ago('2019-11-22 15:22:35');

How Can I display this timestamp format (Sent: 08:23:38 pm) to Since 1min ago

I have a msg system in which the format is (Sent: 08:23:38 pm) and i want it like 1 min ago etc...
this is what i got from here but its not helping me i m not able to fetch the timestamp from the db instead tried coping one of the value from column(timestamp) i.e 1467993620 so basically i want this to work in while loop and when i keep this in while loop i get error
plz help me for this
$time = strtotime(date('h:i:s a','1467993620'));
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
i just posted query with while loop and the echo timestamp hope it helps you
<?php
$req2 = mysql_query('select pm.timestamp, pm.message, user.userID as userid, user.userName, user.userAddress from pm, user where pm.userID="'.$userID.'" and user.userID=pm.user1 order by pm.id2');
while($dn2 = mysql_fetch_array($req2))
{
?>
Sent: <?php date_default_timezone_set('Asia/Kolkata'); echo date('h:i:s a',$dn2['timestamp']); ?> </small> </div>
<br />
<p> <?php echo $dn2['message']; ?> </p>
<?php
}
?>
function time_elapsed_string($ptime)
{
$etime = time() - $ptime;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
Try this format :
<?php
$time = strtotime(date('h:i:s a','1467993620'));
$time = $time - (1 * 60); // MINUS 60 SECONDS.
$date = date("h:i:s A", $time); // NOTICE THE "A".
echo "Sent: $date";
?>
Edit : calling humanTiming with your new code :
<?php
date_default_timezone_set('Asia/Kolkata');
echo "Sent: " . humanTiming( $dn2['timestamp'] ) . " ago.";
?>

Convert YYYY-MM-DD HH-MM-SS to ... months/days/minutes/hours/seconds ago

This question is based on another StackOverFlow question, which is entitled
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…
The ticked answer was unexplained but showed this function:
function time_elapsed_string($ptime)
{
$etime = time() - $ptime;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
The time format I have is YYYY-MM-DD HH-MM-SS, for example: 2015-01-21 19:23:09.
My attempt:
echo $target_time = time_elapsed_string(strtotime("2015-01-21 19:23:09"));
It gives:
45 years ago.
Any time is giving also 45 years old. I don't know how the function works, but any help is appreciated.
This can easily be accomplished using PHP's DateTime and DateDiff objects
$datetime1 = new DateTime();
$datetime2 = new DateTime("2015-01-21 19:23:09");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
For a full description of the format string to pass, check out the documentation.
I found an easy, and efficient solution from http://timeago.yarp.com:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script>
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
</script>
<abbr class="timeago" title="2015-01-21T21:30:00"></abbr>

Unix timestamp to last seen

Could you please help me.
I have a unix timestamp that i want to convert to a last seen function
Example:
current unix timestamp - last seen timestamp = 2 days, 19 hours, 05 min and 81 sec
Try this ,
function ago($mtime)
{
$xtime = time() - $mtime;
if ($xtime < 1)
{
return '0 seconds';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str)
{
$d = $xtime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
}
}}
Hi i found a solution that is must faster and easy to use. Tanx for the reply
function ago($unix)
{
$datetime1 = new DateTime(date("Y-m-d H:i:s", $unix));
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
return $interval->format('%a days, %H hours, %I min and %S sec ');
}
This returns 0 days, 19 hours, 33 min and 31 sec

How to calculate "time ago" in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP How to find the time elapsed since a date time?
How would I get the time ago posted. Just example 3 minutes ago or more than 24 hours ago and 2 days ago out of 2012-07-13 17:55:59.
<?php
function get_timeago( $ptime )
{
$etime = time() - $ptime;
if( $etime < 1 )
{
return 'less than 1 second ago';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $a as $secs => $str )
{
$d = $etime / $secs;
if( $d >= 1 )
{
$r = round( $d );
return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
}
}
}
?>
Usage:
<?php
echo get_timeago( $timestamp );
?>
Also try strtotime()

Categories