Unix timestamp to last seen - php

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

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 to fix Timeago errors

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';
}
}
}

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>

Days, hours, minutes since timestamp

So I have this function, which displays how long it's been since a timestamp.
Right now if it's been let's say 60 minutes, it will go over and show "1 hour ago". If it's been 24 hours it goes over to "1 day ago" and so on. How could I do so that if it's been for example 1 hour, show minutes also?
Like
Event occured 58 minutes ago
3 minutes later
Event occured 1 hour 1 minute ago
26 hours later
Event occured 1 day 2 hour ago
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$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.' ago';
}
}
You need to modify your function like this:
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$result = '';
$counter = 1;
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
if ($counter > 2) break;
$numberOfUnits = floor($time / $unit);
$result .= "$numberOfUnits $text ";
$time -= $numberOfUnits * $unit;
++$counter;
}
return "{$result}ago";
}
Check result on codepad.org.
PHP 5.2.0 introduced DateTime object, which will provide a better solution for the problem.
I am going to use my answer on another question to show how easy it is, though will slightly edit the code:
$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";
$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
// to use current timestamp, use the following:
//$dtCurrent = new DateTime();
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval will return 0 years 0 months 0 days 3 hours 40 minutes 58 seconds
Let's get rid of those zero values now. Good old regex is pretty much useful here:
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
In the end we get exactly what we need: 3 hours 40 minutes 58 seconds
You can use a progressive calculation method such as:
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$tokenValues = array();
foreach($tokens as $tokenValue => $tokenName)
{
if($time > $tokenValue)
{
$tokenValues[$tokenName] = floor($time \ $tokenValue);
$time -= $tikenValue;
}
if(count($tokenValues) == 2)
{
break;
}
}
You should then have only 2 items in tokenValues, adapt as you see fit!
You can find Event occurred time and current time difference using diff. For example see the following code
$time1 = new DateTime('2013-11-21 12:59:00'); // Event occurred time
$time2 = new DateTime(date('Y-m-d H:i:s')); // Current time
$interval = $time1->diff($time2);
echo $interval->y . " Year " .$interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
I don't have time to explain, but this function works as I tested it. In addition, I add a feature to handle time from the current time and future. If you supply this function with exact current time, it will return 'just now'. If you supply it with time from the past, it will return a string ended with 'ago'. If you supply it with time from the future, it will return a string ended with 'later'. Another feature is automatic singular/plural form for the time unit. Try it yourself!
function timeEllapsedFrom($from) {
$time = time() - $from;
$diff = abs($time);
$tokens = array (
'year' => 31536000,
'month' => 2592000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
$result = array();
foreach ($tokens as $id => $length) {
$value = floor($diff/$length);
if ($value) $result[] = "$value $id" . ($value > 1 ? 's' : '');
$diff -= $length*$value;
}
if (!count($result)) return 'just now';
return join(', ', $result) . ($time < 0 ? ' later' : ' ago');
}
You can test it with this code:
echo timeEllapsedFrom(time() - 604800 - 7200 - 480 - 24);
and it will output:
1 week, 2 hours, 8 minutes, 24 seconds ago
Hope it helps!
Using DateTime class. Works with both past and future timestamps, and does correct pluralization when needed:
function elapsedTime($time)
{
date_default_timezone_set('UTC');
$given_time = new DateTime("#$time");
$current_time = new DateTime();
$diff = $given_time->diff($current_time);
$timemap = array('y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second');
$timefmt = array();
foreach ($timemap as $prop => $desc) {
if ($diff->$prop > 0) {
$timefmt[] = ($diff->$prop > 1)
? "{$diff->$prop} {$desc}s"
: "{$diff->$prop} $desc";
}
}
return (!$timefmt)
? 'just now'
: ((count($timefmt) > 1)
? $diff->format(sprintf('%s and %s',
implode(', ', array_slice($timefmt, 0, -1)), end($timefmt)))
: end($timefmt))
. (($given_time < $current_time) ? ' ago' : ' later');
}
Testing:
var_dump(elapsedTime(strtotime('1987-11-25 11:40')));
# string(69) "25 years, 11 months, 29 days, 17 hours, 50 minutes and 42 seconds ago"
var_dump(elapsedTime(time()));
# string(8) "just now"
var_dump(elapsedTime(strtotime('2013-11-25')));
# string(41) "18 hours, 29 minutes and 18 seconds later"

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