PHP Time Since Function

Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...

Use example :

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);

Input can be any supported date and time format.

Output :

4 months ago
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

Function :

function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);

$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;

$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}

if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}

PHP How to find the time elapsed since a date time?

Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right?

This is how I'd go about doing that:

$time = strtotime('2010-04-28 17:25:43');

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 haven't tested that, but it should work.

The result would look like

event happened 4 days ago

or

event happened 1 minute ago

PHP Time Since Function Bug

strtotime uses timezone in your PHP settings. Depending on timezone set, it might convert to the time that is yet to happen. For example, on my ukrainian server, strtotime('2016-02-25 14:35:00') converts to 1456403700, on a server in another timezone (US/Pacific) it converts to 1456439700.

Quote from PHP docs:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

You can add UTC/GMT offset to your datetime (1st param), for example strtotime('2016-02-25 14:35:00 +0800') or ('2016-02-25 14:35:00 GMT+08:00') will convert to 1456382100

php time ago function

This is probably a duplicate, but closing it won't show you what the problem in your code is.

The return statement will immediately return the passed value from the function, and end execution of the function. So you will not ever get past the first run of your foreach loop. Probably what you want to do is something like this, where you build a string up in the loop, and then return it:

$ret = "";
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
$ret .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
return $ret;

I didn't actually check if your code would work, but this is the crux of your problem.

date / time convert to time since php

Below is a function I wrote to do this. Feel free to use it.

/**
* Returns rough (in largest single unit) time elapsed between two times.
* @param int $iTime0 Initial time, as time_t.
* @param int $iTime1 Final time, as time_t. 0=use current time.
* @return string Time elapsed, like "5 minutes" or "3 days" or "1 month".
* You might print "ago" after this return if $iTime1 is now.
* @author Dan Kamins - dos at axonchisel dot net
*/
function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
{
if ($iTime1 == 0) { $iTime1 = time(); }
$iTimeElapsed = $iTime1 - $iTime0;

if ($iTimeElapsed < (60)) {
$iNum = intval($iTimeElapsed); $sUnit = "second";
} else if ($iTimeElapsed < (60*60)) {
$iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
} else if ($iTimeElapsed < (24*60*60)) {
$iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
} else if ($iTimeElapsed < (30*24*60*60)) {
$iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
} else if ($iTimeElapsed < (365*24*60*60)) {
$iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
} else {
$iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
}

return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "");
}

To use this func, you'd need to first convert your times to time_t format (integer #seconds since the "epoch"). Either of these PHP functions will probably help with that: http://php.net/strptime or http://php.net/strtotime.

How can I limit time ago function to under 12 months and post actual date for year or later?

You need to check against the time of one year ago - not against the array $tokens. You can get the time of one year ago from strtotime('1 year ago'). If that is lesser than time()-$time (the current timestamp minus the $time timestamp), then the input-time is more than one year 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'
);

if (time()-$time < strtotime("1 year ago")) {
return date("F jS, Y", time()-$time);
} else {
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
}
}
}

echo 'event happened '.humanTiming($time).'';
  • Live demo at https://3v4l.org/UH9df

Time Ago Function PHP Not Working?

Change the first line of your function so you're subtracting seconds from seconds:

$etime = time() - strtotime($ptime);

How to display time in x days ago in php

try this

$date1 = strtotime('2014-12-06 15:25:08');
$date2 = strtotime(date('Y-m-d H:i:s'));
$seconds_diff = $date2 - $date1;

echo round(abs($seconds_diff) / 60,2). " mins ago";


Related Topics



Leave a reply



Submit