WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder



Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP function for displaying relative dates in a human readable format
Categories : PHP, Date Time Click here to Update Your Picture
John McClumpha
Date : Mar 03rd 2008
Grade : 2 of 5 (graded 3 times)
Viewed : 12273
File : 4769.php
Images : No Images for this code example.
Search : More code by John McClumpha
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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);
?>



If you want to create select buttons featuring current date this example will show you how...
Categories : Date Time, HTML and PHP, PHP
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
Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases
Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June).
Categories : Calendar, Date Time, PHP
Spreadsheet Date Calculations in PHP
Categories : PHP, Date Time, Excel
table calendar pretty much does what cal(1) does - prints out a calendar.
Categories : Date Time, PHP, Calendar
Website colour changer
Categories : PHP, HTML and PHP, Date Time
Simple PHP control CSS Calendar
Categories : PHP, HTML and PHP, Calendar, Date Time, CSS
A PHP Script that shows how to use FTP to run a shell script, read two local files and update data in a database.
Categories : PHP, Filesystem, FTP, Date Time, Databases
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
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
Convert date's in YYYY-MM-DD (i.e. mysql format) into PHP3 timestamps. Also Find the difference in days between two PHP3 timestamps.
Categories : HTML and PHP, PHP, MySQL, Date Time
Date Validation using JavaScript. Intended to use it as checkdate() from PHP.
Categories : Java Script, Date Time, PHP
List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP
a class that uses microtime() to provide easy calculation of elapsed times
Categories : Date Time, PHP, PHP Classes