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



Script loading time
Categories : PHP, Beginner Guides, Date Time
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
CALENDAR - easy calendar-navigation with PHP
Categories : PHP, Date Time, HTML and PHP, Calendar
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
Find if a year is leap.
Categories : PHP, Date Time, Beginner Guides, Data Validation
Kewl Date Example
Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides
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
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
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
Simple function to return the number of days in a time span between 2 given dates.
Categories : PHP, Date Time, MySQL, Databases
How to Insert a Date Format Into MySQL from PHP
Categories : PHP, Databases, MySQL, Date Time, Beginner Guides
Function that returns an array with the 7 dates of the week that belong to the supplied date.
Categories : PHP, Date Time, Arrays
time() function with microseconds
Categories : Date Time, PHP
Calendars to choose a range of dates , reservation events ...
Categories : PHP, Calendar, Java Script, Date Time
checkdate -- Validate a gregorian date/time
Categories : PHP, PHP Functions, Date Time