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"
Related
I have time and date from my DB and I tried to echo the same in "minutes/hrs Ago" form.
like if I have text posted 2 days back, it shows as "2 days ago" by taking the time and date.
The code works fine, However, it doesn't vary for different time regions.
my code goes as:
<?php
$mytimestring = '2018-02-27 07:57:19';
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':'');
}
}
echo humanTiming( strtotime($mytimestring) );
?>
It only works for that given time and doesn't vary for IST or any other.
Any helps is greatly appreciated..
I am using this function
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':'');
}
}
which is a slightly modified version of the original as i swapped these to around $time - time() so that the function would give me a result which would tell me a time until a specific date is not a time when a specific date was.
what i am trying to do is when the time is less than 1 second have it display "expired" instead of the current default "1 second"
any ideas how i can do this?
Many thanks
Luke
$time = ($time<1)? 1 : $time;
replace with
if ($time < 1) { return 'expired';}
You can do it before the loop:
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',
);
if($time < 1 ) return 'expired';
foreach ($tokens as $unit => $text)
{
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
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.";
?>
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>
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