|
|
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);
?> | | |
|
| Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL | | | Website colour changer Categories : PHP, HTML and 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 | | | Simple graphic clock/watch generation using gd library. Categories : Graphics, Date Time, PHP | | | A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed. Categories : Date Time, PHP, Complete Programs, Calendar | | | PHP Calendar Web App Categories : PHP, Databases, MySQL, Date Time, Calendar | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | This script obtains the current Universal Time from the National Institute of Standards and Technology of the United States of America. Categories : PHP, Date Time | | | time() function with microseconds Categories : Date Time, PHP | | | Bs_StopWatch is a class to measure time intervals in microseconds.
Categories : PHP, Date Time, PHP Classes | | | checkdate -- Validate a gregorian date/time Categories : PHP, PHP Functions, Date Time | | | Validator - A PHP class that can can be used for validating Email IDs and Dates Categories : PHP, PHP Classes, Data Validation, Email, Date Time | | | Script loading time Categories : PHP, Beginner Guides, Date Time | | | formating MySQL Timestamp to get a propper Output.
related : Timestamp, ereg_replace, mysql, substr Categories : MySQL, PHP, Date Time, Databases | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | |
| |
| |
|