Here's a handy PHP function I use for comparing times and displaying the difference in a human-readable format.
The function itself returns the nearest approximate time based on the largest value from: decades, years, months, weeks, days, hours, minutes or seconds
<?php
function RelativeTime ( $timestamp ){
$difference = time () - $timestamp ;
$periods = array( "sec" , "min" , "hour" , "day" , "week" , "month" , "years" , "decade" );
$lengths = array( "60" , "60" , "24" , "7" , "4.35" , "12" , "10" );
if ( $difference > 0 ) { // this was in the past
$ending = "ago" ;
} else { // this was in the future
$difference = - $difference ;
$ending = "to go" ;
}
for( $j = 0 ; $difference >= $lengths [ $j ]; $j ++) $difference /= $lengths [ $j ];
$difference = round ( $difference );
if( $difference != 1 ) $periods [ $j ].= "s" ;
$text = "$difference $periods[$j] $ending" ;
return $text ;
}
?>
Usage Example
<?php
echo RelativeTime ( $timestamp );
?>
Function to convert Arabic numbers into Roman Numerals Categories : Algorithms , PHP , Date Time Customizable Calendar Class Categories : HTML and PHP , Date Time , PHP , PHP Classes , Calendar Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP , Arrays , Date Time , Databases , MySQL function to generate calendars on the fly.
Categories : Calendar , PHP , Date Time Change the background color of a website daily dynamically using the php date function to get the current day of the week and depending on that day, set the background color for the web page. Categories : PHP , Date Time , Beginner Guides , Web Design Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP , Date Time , Strings , MySQL , Databases PHP Calendar Web App Categories : PHP , Databases , MySQL , Date Time , Calendar calculus of the eastersunday Categories : BC math , PHP , Date Time , Databases Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June). Categories : Calendar , Date Time , PHP Monthly and Daily Upcoming Events calendar. Categories : Date Time , PostgreSQL , PHP , Calendar , Databases Website colour changer Categories : PHP , HTML and PHP , Date Time PHP Calendar Categories : PHP , Calendar , Date Time , Java Script , CSS PHP Round Clock - Must have Gif support to use this. Categories : PHP , Date Time , Graphics This script contains 2 functions: 1 to create html select object based on your own customer date format entry- "M d Y h:i.... etc". The second function processes the select object on submit back to unix time. Categories : PHP , Calendar , Date Time , HTML and PHP Select with current month Categories : PHP , HTML and PHP , Date Time , Arrays