Converting Timestamp to Time Ago in PHP E.G 1 Day Ago, 2 Days Ago...

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

Converting timestamp to time ago in PHP - Plurality

Here is a version of that function which uses an array for each period to indicate the word to use for singular and plural periods:

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;

$periods = array(
'y' => ['jaar', 'jaren'],
'm' => ['maand', 'maanden'],
'w' => ['week', 'weken'],
'd' => ['dag', 'dagen'],
'h' => ['uur', 'uren'],
'i' => ['minuut', 'minuten'],
's' => ['seconde', 'seconden']
);

$parts = array();
foreach ($periods as $k => &$v) {
if ($diff->$k) {
$parts[] = $diff->$k . ' ' . $v[$diff->$k > 1];
}
}

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

Sample usage:

echo time_elapsed_string('2020-03-21 00:30:16') . PHP_EOL;
echo time_elapsed_string('2020-03-21 00:30:16', true) . PHP_EOL;

Output:

2 maanden ago
2 maanden, 1 week, 3 dagen, 14 uren, 6 minuten, 55 seconden ago

Demo on 3v4l.org

Converting timestamp to time ago in PHP

Here's a function i wrote for you.

It uses DateTime class, so PHP version must be >= 5.3.0.

Read the comments in function to understand how it works.

function timeago($time, $tense='ago') {
// declaring periods as static function var for future use
static $periods = array('year', 'month', 'day', 'hour', 'minute', 'second');

// checking time format
if(!(strtotime($time)>0)) {
return trigger_error("Wrong time format: '$time'", E_USER_ERROR);
}

// getting diff between now and time
$now = new DateTime('now');
$time = new DateTime($time);
$diff = $now->diff($time)->format('%y %m %d %h %i %s');
// combining diff with periods
$diff = explode(' ', $diff);
$diff = array_combine($periods, $diff);
// filtering zero periods from diff
$diff = array_filter($diff);
// getting first period and value
$period = key($diff);
$value = current($diff);

// if input time was equal now, value will be 0, so checking it
if(!$value) {
$period = 'seconds';
$value = 0;
} else {
// converting days to weeks
if($period=='day' && $value>=7) {
$period = 'week';
$value = floor($value/7);
}
// adding 's' to period for human readability
if($value>1) {
$period .= 's';
}
}

// returning timeago
return "$value $period $tense";
}

don't forget to set timezone you work

date_default_timezone_set('UTC');

use it

echo timeago('1981-06-07'); // 34 years ago
echo timeago(date('Y-m-d H:i:s')); // 0 seconds ago

etc.

Converting this date into time ago

Time ago function

function time_ago($date) {
if (empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if (empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if ($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}

To use this function, simply call:

<?php echo time_ago($mydate); ?>

source

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";

If less than 1 month convert timestamp to time ago in PHP

You can then check if the timestamp assigned to $posted is greater i.e. more recent than the timestamp of a month ago.

if it is more recent, then convert it to a human time ago and append the word ago if its not more recent then use the date('d F Y) or whatever format you want long dates to appear in to convert the timestamp back to a readable date.

<?php
$posted = get_post_time();
$date = $posted >= strtotime('-1 month') ? human_time_diff($posted) . ' ago' : date('d F Y', $posted);
?>

<?php echo $date; ?>

Returns the expected date format of time ago for dates less than 1 month ago, and the full date for dates longer than 1 month ago...

See image for example.
wordpress example

Converting TimeStamp to readable time ago in PHP and Android

The timeAgo function should return value instead of echo.

And you forgot to call $ads['date'] = timeAgo($time_ago); and that is why you have the strtotime value in your JSON instead of the converted value to ago.

EDIT

To return the value from your timeAgo() function simply do this:

return $seconds . " seconds ago";


Related Topics



Leave a reply



Submit