PHP and MySQL Code

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
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.
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 : 12822
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

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";
$j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
$difference = round($difference);
$difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";

Usage Example
echo RelativeTime($timestamp);

Bs_StopWatch is a class to measure time intervals in microseconds.
Categories : PHP, Date Time, PHP Classes
Convert MYSQL timestamp (14) fields back to UNIX epoch (seconds) format for display with the date() function. "revertTimeStamp($timestamp)"
Categories : PHP, Date Time, MySQL
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
Simple function to return the number of days in a time span between 2 given dates.
Categories : PHP, Date Time, MySQL, Databases
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
enhanced date picker with jcript checking for a dynamic date input
Categories : PHP, Java Script, Date Time, Calendar, Arrays
A wrapper function to format dates coming from a databases with the same syntax as PHP's date() function.
Categories : Date Time, Databases, PHP
Calendars to choose a range of dates , reservation events ...
Categories : PHP, Calendar, Java Script, 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
This code consists of two parts, the first part is a .html file that uses a form to pass 3 date variables into day_of_week.php3 You input the mm/dd/yyyy of the day then it prints what day of the week that day falls on.
Categories : Date Time, PHP, Complete Programs
Example of using the pcCalendar class, article 1468 on Calendar example.
Categories : PHP, Date Time, PHP Classes, Calendar
Functions that will format a date in either long or short format from a string.
Categories : Date Time, Strings, PHP
Function to convert Arabic numbers into Roman Numerals
Categories : Algorithms, 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