note 90989 modified in ref.datetime by danbrown
| From: | danbrown@php.net | Date: | Thu, 22 Oct 2009 13:18:45 +0000 |
| Subject: | note 90989 modified in ref.datetime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161979@lists.php.net to get a copy of this message | ||
This is an easily extendable and pretty way to output human-readable date differences such as
"1 day 2 hours ago", "6 months ago", "3 years 7 months 14 days 1 hour 4
minutes 16 seconds" etc etc.
Change "$levels = 2;" to whatever you want. A value of 1 will limit to only one number in
the result ("3 days ago"). A value of 3 would result in up to three ("3 days 1 hour 2
minutes ago")
It can be used in the following ways:
echo compare_dates($start_date,$end_date);
echo compare_dates($end_date,$start_date);
echo compare_dates($start_date); //end date will be assumed as time();
<?php
function compare_dates($date1, $date2 = time())
{
$blocks = array(
array('name'=>'year','amount' => 60*60*24*365 ),
array('name'=>'month','amount' => 60*60*24*31 ),
array('name'=>'week','amount' => 60*60*24*7 ),
array('name'=>'day','amount' => 60*60*24 ),
array('name'=>'hour','amount' => 60*60 ),
array('name'=>'minute','amount' => 60 ),
array('name'=>'second','amount' => 1 )
);
$diff = abs($date1-$date2);
$levels = 2;
$current_level = 1;
$result = array();
foreach($blocks as $block)
{
if ($current_level > $levels) {break;}
if ($diff/$block['amount'] >= 1)
{
$amount = floor($diff/$block['amount']);
if ($amount>1) {$plural='s';} else {$plural='';}
$result[] = $amount.' '.$block['name'].$plural;
$diff -= $amount*$block['amount'];
$current_level++;
}
}
return implode(' ',$result).' ago';
}
?>
[EDIT BY danbrown AT php DOT net: Contains a bugfix supplied by (jorge AT dontspam DOT com) on
22-OCT-2009.]
--was--
This is an easily extendable and pretty way to output human-readable date differences such as
"1 day 2 hours ago", "6 months ago", "3 years 7 months 14 days 1 hour 4
minutes 16 seconds" etc etc.
Change "$levels = 2;" to whatever you want. A value of 1 will limit to only one number in
the result ("3 days ago"). A value of 3 would result in up to three ("3 days 1 hour 2
minutes ago")
It can be used in the following ways:
echo compare_dates($start_date,$end_date);
echo compare_dates($end_date,$start_date);
echo compare_dates($start_date); //end date will be assumed as time();
<?php
function compare_dates($date1, $date2 = time())
{
$blocks = array(
array('name'=>'year','amount' => 60*60*24*365 ),
array('name'=>'month','amount' => 60*60*24*31 ),
array('name'=>'week','amount' => 60*60*24*7 ),
array('name'=>'day','amount' => 60*60*24 ),
array('name'=>'hour','amount' => 60*60 ),
array('name'=>'minute','amount' => 60 ),
array('name'=>'second','amount' => 1 )
);
$diff = abs($date1-$date2);
$levels = 2;
$current_level = 1;
$result = array();
foreach($blocks as $block)
{
if ($current_level > $levels) {break;}
if ($diff/$block['amount'] >= 1)
{
$amount = floor($diff/$block['amount']);
if ($amount>1) {$plural='s';} else {$plural='';}
$result[] = $amount.' '.$block['name'].$plural;
$diff -= $amount*$block['amount'];
}
}
return implode(' ',$result).' ago';
}
?>
http://php.net/manual/en/ref.datetime.php