Are there any classes/functions written in php publicly available that will take a timestamp, and return the time passed since then in number of days, months, years etc? Basically i want the same function that generates the time-since-posted presented together with each entry on this site (and on digg and loads of other sites).
-
possible duplicate of http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago or http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Glavić Nov 13 '13 at 22:20
-
Could you just translate the [C# code that SO uses](https://stackoverflow.com/questions/11/how-do-i-calculate-relative-time)? – A. Rex Jan 11 '09 at 05:09
5 Answers
This is written as a wordpress plugin but you can extract the relevant PHP code no problem: Fuzzy date-time

- 6,153
- 1
- 22
- 19
Here is a Zend Framework ViewHelper I wrote to do this, you could easily modify this to not use the ZF specific code:
/**
* @category View_Helper
* @package Custom_View_Helper
* @author Chris Jones <leeked@gmail.com>
* @license New BSD License
*/
class Custom_View_Helper_HumaneDate extends Zend_View_Helper_Abstract
{
/**
* Various time formats
*/
private static $_time_formats = array(
array(60, 'just now'),
array(90, '1 minute'), // 60*1.5
array(3600, 'minutes', 60), // 60*60, 60
array(5400, '1 hour'), // 60*60*1.5
array(86400, 'hours', 3600), // 60*60*24, 60*60
array(129600, '1 day'), // 60*60*24*1.5
array(604800, 'days', 86400), // 60*60*24*7, 60*60*24
array(907200, '1 week'), // 60*60*24*7*1.5
array(2628000, 'weeks', 604800), // 60*60*24*(365/12), 60*60*24*7
array(3942000, '1 month'), // 60*60*24*(365/12)*1.5
array(31536000, 'months', 2628000), // 60*60*24*365, 60*60*24*(365/12)
array(47304000, '1 year'), // 60*60*24*365*1.5
array(3153600000, 'years', 31536000), // 60*60*24*365*100, 60*60*24*365
);
/**
* Convert date into a pretty 'human' form
* Now with microformats!
*
* @param string|Zend_Date $date_from Date to convert
* @return string
*/
public function humaneDate($date_from)
{
$date_to = new Zend_Date(null, Zend_Date::ISO_8601);
if (!($date_from instanceof Zend_Date)) {
$date_from = new Zend_Date($date_from, Zend_Date::ISO_8601);
}
$dateTo = $date_to->getTimestamp(); // UnixTimestamp
$dateFrom = $date_from->getTimestamp(); // UnixTimestamp
$difference = $dateTo - $dateFrom;
$message = '';
if ($dateFrom <= 0) {
$message = 'a long time ago';
} else {
foreach (self::$_time_formats as $format) {
if ($difference < $format[0]) {
if (count($format) == 2) {
$message = $format[1] . ($format[0] === 60 ? '' : ' ago');
break;
} else {
$message = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
break;
}
}
}
}
return sprintf('<abbr title="%sZ">%s</abbr>',
$date_from->get('YYYY-MM-ddTHH:mm:ss'),
$message
);
}
}

- 11,803
- 8
- 45
- 61
I'm not sure there will be classes for that but I've found on Google a couple of methods to achieve what you want:
- http://www.phpbuilder.com/board/showpost.php?p=10100477&postcount=2
- http://subesh.com.np/2008/06/calculating-the-difference-between-timestamps-in-php/
- http://snipplr.com/view/10674/esupergood--formattime-function-tweak/
Maybe one of them fits or needs or you can easily adapt it.

- 16,546
- 57
- 163
- 275
Brother Google knows the answer:
This has been asked before:
How to calculate the difference between two dates using PHP?
This is the best version I have seen (for human readable format):
PHP 5+ has something now built in:
http://php.net/manual/en/datetime.diff.php
I personally was looking for calculating the number of days (as a decimal) so I can then subset into years, etc.
function daysDifference($d1,$d2)
{
$ts2 = strtotime($d1);
$ts1 = strtotime($d2);
$seconds = abs($ts2 - $ts1); # difference will always be positive
$days = $seconds/60/60/24;
return $days;
}
This function returns a numeric array. You may extract years, months, days, hours, minutes and seconds. e.g. echo $result[3] gets you hours and echo $result[4] gets you minutes. (I have borrowed this code). cheers!
function dateDiff($time1, $time2, $precision = 6)
{
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year', 'month', 'day', 'hour', 'minute', 'second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value >= 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value; // . " " . $interval;
$count++;
}
}
// Return string with times
//return implode(", ", $times);
return $times;
}

- 1
- 1