How to display 12 minutes ago etc in a PHP webpage?
Here is the php code for the same:
function time_since($since) {
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
array(1 , 'second')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print;
}
The function takes the number of seconds as input and outputs text such as:
- 10 seconds
- 1 minute
etc
date to time ago ? without useing strtime?
function nicetime($fromDate, $toDate = NULL, $precision = -1, $separator = ', ', $divisors = NULL) {
if ( is_null( $toDate ) ) {
$toDate = $this->date_get('Asia/Jakarta');
}
// Determine the difference between the largest and smallest date
$dates = array(strtotime($fromDate), strtotime($toDate));
$difference = max($dates) - min($dates);
// Return the formatted interval
return $this->format_interval($difference, $precision, $separator, $divisors);
}
/**
* Formats any number of seconds into a readable string
*
* @param int Seconds to format
* @param string Seperator to use between divisors
* @param int Number of divisors to return, ie (3) gives '1 Year, 3 Days, 9 Hours' whereas (2) gives '1 Year, 3 Days'
* @param array Set of Name => Seconds pairs to use as divisors, ie array('Year' => 31536000)
* @return string Formatted interval
*/
function format_interval($seconds, $precision = -1, $separator = ', ', $divisors = NULL)
{
// Default set of divisors to use
if(!isset($divisors)) {
$divisors = Array(
'Year' => 31536000,
'Month' => 2628000,
'Day' => 86400,
'Hour' => 3600,
'Minute' => 60,
'Second' => 1);
}
arsort($divisors);
// Iterate over each divisor
foreach($divisors as $name => $divisor)
{
// If there is at least 1 of thie divisor's time period
if($value = floor($seconds / $divisor)) {
// Add the formatted value - divisor pair to the output array.
// Omits the plural for a singular value.
if($value == 1)
$out[] = "$value $name";
else
$out[] = "$value {$name}s";
// Stop looping if we've hit the precision limit
if(--$precision == 0)
break;
}
// Strip this divisor from the total seconds
$seconds %= $divisor;
}
// FIX
if (!isset($out)) {
$out[] = "0" . $name;
}
var_dump($out);
// Join the value - divisor pairs with $separator between each element
return implode($separator, $out);
}
Subtracting time to post how many minutes ago
I usually use this function. Use it like this time_ago('2014-12-03 16:25:26')
function time_ago($date){
$retval = NULL;
$granularity=2;
$date = strtotime($date);
$difference = time() - $date;
$periods = array('decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1);
foreach ($periods as $key => $value)
{
if ($difference >= $value)
{
$time = round($difference/$value);
$difference %= $value;
$retval .= ($retval ? ' ' : '').$time.' ';
$retval .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') { break; }
}
return $retval.' 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';
}
date to time ago ? without useing strtime?
function nicetime($fromDate, $toDate = NULL, $precision = -1, $separator = ', ', $divisors = NULL) {
if ( is_null( $toDate ) ) {
$toDate = $this->date_get('Asia/Jakarta');
}
// Determine the difference between the largest and smallest date
$dates = array(strtotime($fromDate), strtotime($toDate));
$difference = max($dates) - min($dates);
// Return the formatted interval
return $this->format_interval($difference, $precision, $separator, $divisors);
}
/**
* Formats any number of seconds into a readable string
*
* @param int Seconds to format
* @param string Seperator to use between divisors
* @param int Number of divisors to return, ie (3) gives '1 Year, 3 Days, 9 Hours' whereas (2) gives '1 Year, 3 Days'
* @param array Set of Name => Seconds pairs to use as divisors, ie array('Year' => 31536000)
* @return string Formatted interval
*/
function format_interval($seconds, $precision = -1, $separator = ', ', $divisors = NULL)
{
// Default set of divisors to use
if(!isset($divisors)) {
$divisors = Array(
'Year' => 31536000,
'Month' => 2628000,
'Day' => 86400,
'Hour' => 3600,
'Minute' => 60,
'Second' => 1);
}
arsort($divisors);
// Iterate over each divisor
foreach($divisors as $name => $divisor)
{
// If there is at least 1 of thie divisor's time period
if($value = floor($seconds / $divisor)) {
// Add the formatted value - divisor pair to the output array.
// Omits the plural for a singular value.
if($value == 1)
$out[] = "$value $name";
else
$out[] = "$value {$name}s";
// Stop looping if we've hit the precision limit
if(--$precision == 0)
break;
}
// Strip this divisor from the total seconds
$seconds %= $divisor;
}
// FIX
if (!isset($out)) {
$out[] = "0" . $name;
}
var_dump($out);
// Join the value - divisor pairs with $separator between each element
return implode($separator, $out);
}
convert time stamp to time ago in php?
You should use the DateTime
class to get the difference between 2 times, ie;
$time1 = new DateTime('2014-10-06 09:00:59');
$now = new DateTime();
$interval = $time1->diff($now,true);
and then use that difference (which is a DateInterval
object, $interval
) to find the smallest time difference like this;
if ($interval->y) echo $interval->y . ' years';
elseif ($interval->m) echo $interval->m . ' months';
elseif ($interval->d) echo $interval->d . ' days';
elseif ($interval->h) echo $interval->h . ' hours';
elseif ($interval->i) echo $interval->i . ' minutes';
else echo "less than 1 minute";
which should echo (at time of writing) 13 hours
.
Hope this helps.
date to time ago ? without useing strtime?
function nicetime($fromDate, $toDate = NULL, $precision = -1, $separator = ', ', $divisors = NULL) {
if ( is_null( $toDate ) ) {
$toDate = $this->date_get('Asia/Jakarta');
}
// Determine the difference between the largest and smallest date
$dates = array(strtotime($fromDate), strtotime($toDate));
$difference = max($dates) - min($dates);
// Return the formatted interval
return $this->format_interval($difference, $precision, $separator, $divisors);
}
/**
* Formats any number of seconds into a readable string
*
* @param int Seconds to format
* @param string Seperator to use between divisors
* @param int Number of divisors to return, ie (3) gives '1 Year, 3 Days, 9 Hours' whereas (2) gives '1 Year, 3 Days'
* @param array Set of Name => Seconds pairs to use as divisors, ie array('Year' => 31536000)
* @return string Formatted interval
*/
function format_interval($seconds, $precision = -1, $separator = ', ', $divisors = NULL)
{
// Default set of divisors to use
if(!isset($divisors)) {
$divisors = Array(
'Year' => 31536000,
'Month' => 2628000,
'Day' => 86400,
'Hour' => 3600,
'Minute' => 60,
'Second' => 1);
}
arsort($divisors);
// Iterate over each divisor
foreach($divisors as $name => $divisor)
{
// If there is at least 1 of thie divisor's time period
if($value = floor($seconds / $divisor)) {
// Add the formatted value - divisor pair to the output array.
// Omits the plural for a singular value.
if($value == 1)
$out[] = "$value $name";
else
$out[] = "$value {$name}s";
// Stop looping if we've hit the precision limit
if(--$precision == 0)
break;
}
// Strip this divisor from the total seconds
$seconds %= $divisor;
}
// FIX
if (!isset($out)) {
$out[] = "0" . $name;
}
var_dump($out);
// Join the value - divisor pairs with $separator between each element
return implode($separator, $out);
}
Display time ago instead of datetime in PHP Codeigniter
The Date Helper's timespan()
method just does that:
The most common purpose for this function is to show how much time has elapsed from some point in time in the past to now.
Given a timestamp, it will show how much time has elapsed in this format:
1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
So, in your example, all you need to do is convert your date to a timestamp and do something like this:
$post_date = '13436714242';
$now = time();
// will echo "2 hours ago" (at the time of this post)
echo timespan($post_date, $now) . ' ago';
Related Topics
Push Notifications from Server to User with PHP/Javascript
Utc Date/Time String to Timezone
What Is Salt and How to Use It
Setting a PHP $_Session['Var'] Using Jquery
Handling Put/Delete Arguments in PHP
SQL Query, Select Nearest Places by a Given Coordinates
File_Get_Contents When Url Doesn't Exist
Best Way to Access Exchange Using PHP
How to Create a Facade Class with Laravel
How to Access an Object Property Named as a Variable in PHP
PHP Get PDF File from Base64 Encoded Data String
Oauth Implementation in Netsuite Using PHP