|
|
|
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);
?> | | |
|
| Find the day of the week for any given year/month/day. Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | | | Shows the current time as a PNG-image. This script does not use the GD
library. You can use it as a benchmark (because it's slow), or as a quick
reference for implementing a simple PNG-file generator. Categories : Graphics, Zlib, Calendar, PHP, Date Time | | | Local-to-user date and time display regardless of time zone or where the website's server is located Categories : PHP, Date Time, HTML and PHP, Java Script | | | Date Validation using JavaScript. Intended to use it as checkdate() from PHP. Categories : Java Script, Date Time, PHP | | | Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields Categories : MySQL, Date Time, PHP, Databases | | | CALENDAR - easy calendar-navigation with PHP Categories : PHP, Date Time, HTML and PHP, Calendar | | | Find if a year is leap. Categories : PHP, Date Time, Beginner Guides, Data Validation | | | Calendar using Date function Categories : HTML and PHP, PHP, Date Time, Calendar | | | Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used. Categories : HTML and PHP, PHP, Date Time | | | Count how many weeks in the month have a specified day, such as Mon, Tue, etc. Var avail - number of days - first day name of the month, occurrences of Sun, occurrences of Mon, etc. Allows you to calculate number of working hours exclude Holidays. Categories : Calendar, Date Time, PHP, Databases, MySQL | | | Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP, Date Time, Strings, MySQL, Databases | | | PHP Calendar Categories : PHP, Calendar, Date Time, Java Script, CSS | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example. Categories : PHP, Date Time, PHP Classes, Calendar | |
|
|
|