andy's php projects
Script and site usage:
All of the scripts on this site are provided free for non-commercial, personal or non-profit use. If you wish to use any of these in a commercial system, please refer to the payment plan.

Support:
Support for each of the scripts is given, free of charge, on the message board. Each script has its own message board which can be accessed by clicking on the 'project forum' link (beneath the description of each project).


Proudly hosted with Dreamhost

Save $20 when signing up for Dreamhost by using the promo code amnuts20


If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh to encourage me to continue? Every little helps!

Zend Certified Engineer logo


Date/Time Functions

Provides a number of date and time related functions.

class.datetime.php

A new version adding extra functionality. Please be aware that the name of the class has change, and you no longer need to instantiate it.

<?php

// class.datetime.php
// version 2.0.0, 2004-07-29
//
// Description
//
// A PHP library of helpful date methods.  This class does not
// need to be instantiated.
//
// Author
//
// Andrew Collington, 2004
// php@amnuts.com, http://php.amnuts.com/
//
// Feedback
//
// There is message board at the following address:
//
//    http://php.amnuts.com/forums/index.php
//
// Please use that to post up any comments, questions, bug reports, etc.  You
// can also use the board to show off your use of the script.
//
// License
//
// This class is available free of charge for personal or non-profit work.  If
// you are using it in a commercial setting, please contact php@amnuts.com for
// payment and licensing terms.
//
// Support
//
// If you like this script, or any of my others, then please take a moment
// to consider giving a donation.  This will encourage me to make updates and
// create new scripts which I would make available to you.  If you would like
// to donate anything, then there is a link from my website to PayPal.
//


class DateTime
{
    
/**
     * Compares two dates.
     *
     * Returns:
     *
     *     < 0 if date1 is less than date2;
     *     > 0 if date1 is greater than date2;
     *     0 if they are equal. 
     *
     * @return int
     * @param  string|timestamp $date1
     * @param  string|timestamp $date2
     */
    
function compareDates($date1$date2)
    {
        if (!
is_numeric($date1)) {
            
$date1 DateTime::timeStringToStamp($date1);
        }
        if (!
is_numeric($date2)) {
            
$date2 DateTime::timeStringToStamp($date2);
        }
        if (
$date1 $date2) {
            return -
1;
        } else if (
$date1 $date2) {
            return 
1;
        } else {
            return 
0;
        }
    }

    
/**
     * Converts a date/time string to Unix timestamp
     *
     * @return timestamp
     * @param  string $string
     */
    
function timeStringToStamp($string)
    {
        return 
strtotime($string);
    }

    
/**
     * Converts Unix timestamp to a date/time string using format given
     *
     * Special options can be passed for the format parameter.  These are
     * set format types.  The options currently include:
     *
     *     o mysql
     *
     * If the time parameter isn't supplied, then the current local time
     * will be used.
     *
     * @return string
     * @param  integer $time
     * @param  string  $format
     */
    
function timeStampToString($time 0$format 'Y-m-d H:i:s')
    {
        if (
$format == 'mysql') {
            
$format 'Y-m-d H:i:s';
        }
        
        if (
$time == 0) {
            
$time time();
        }
        
        return 
date($format$time);
    }

    
/**
     * Converts a Unix timestamp or date/time string to a specific format.
     *
     * Special options can be passed for the format parameter.  These are
     * set format types.  The options currently include:
     *
     *     o mysql
     *
     * If the time parameter isn't supplied, then the current local time
     * will be used.
     *
     * @return string
     * @param  integer|string $time
     * @param  string         $format
     * @see    timeStringToStamp()
     * @see    timeStampToString()
     */
    
function timeFormat($time 0$format 'Y-m-d H:i:s')
    {
        if (!
is_numeric($time)) {
            
$time DateTime::timeStringToStamp($time);
        }
        
        if (
$time == 0) {
            
$time time();
        }

        return 
DateTime::timeStampToString($time$format);
    }

    
/**
     * Converts a Unix timestamp or date/time string to a human-readable 
     * format, such as '1 day, 2 hours, 42 mins, and 52 secs'
     *
     * Based on the word_time() function from PG+ (http://pgplus.ewtoo.org)
     *
     * @return string
     * @param  integer|string $time
     * @see    timeStringToStamp()
     */
    
function timeToHumanReadable($time 0)
    {
        if (!
is_numeric($time)) {
            
$time DateTime::timeStringToStamp($time);
        }

        if (
$time == 0) {
            return 
'no time at all';
        } else {
            if (
$time 0) {
                
$neg 1;
                
$time $time;
            } else {
                
$neg 0;
            }
    
            
$days $time 86400;
            
$days floor($days);
            
$hrs  = ($time 3600) % 24;
            
$mins = ($time 60) % 60;
            
$secs $time 60;
    
            
$timestring '';
            if (
$neg) {
                
$timestring .= 'negative ';
            }
            if (
$days) {
                
$timestring .= "$days day" . ($days == '' 's');
                if (
$hrs || $mins || $secs) {
                    
$timestring .= ', ';
                }
            }
            if (
$hrs) {
                
$timestring .= "$hrs hour" . ($hrs == '' 's');
                if (
$mins && $secs) {
                    
$timestring .= ', ';
                }
                if ((
$mins && !$secs) || (!$mins && $secs)) {
                    
$timestring .= ' and ';
                }
            }
            if (
$mins) {
                
$timestring .= "$mins min" . ($mins == '' 's');
                if (
$mins && $secs) {
                    
$timestring .= ', ';
                }
                if (
$secs) {
                    
$timestring .= ' and ';
                }
            }
            if (
$secs) {
                
$timestring .= "$secs sec" . ($secs == '' 's');
            }
            return 
$timestring;
        }
    }

    
/**
     * Give a slightly more fuzzy time string. such as: yesterday at 3:51pm
     *     
     *
     * @return string
     * @param  integer|string $time
     * @see    timeStringToStamp()
     */
    
function fuzzyTimeString($time 0)
    {
        if (!
is_numeric($time)) {
            
$time DateTime::timeStringToStamp($time);
        }

        
$now time();
        
$sodTime mktime(000date('m'$time), date('d'$time), date('Y'$time));
        
$sodNow  mktime(000date('m'$now), date('d'$now), date('Y'$now));
        
        if (
$sodNow == $sodTime) {
            return 
'today at ' date('g:ia'$time); // check 'today'
        
} else if (($sodNow $sodTime) <= 86400) {
            return 
'yesterday at ' date('g:ia'$time); // check 'yesterday'
        
} else if (($sodNow $sodTime) <= 432000) {
            return 
date('l \a\\t g:ia'$time); // give a day name if within the last 5 days
        
} else if (date('Y'$now) == date('Y'$time)) {
            return 
date('M j \a\\t g:ia'$time); // miss off the year if it's this year
        
} else {
            return 
date('M j, Y \a\\t g:ia'$time); // return the date as normal
        
}
    }
    
}

?>
6.64kb in size
last modified May 6, 2006 at 9:54pm
8767 views, 5962 downloads

test.datetime.php

A simple test script showing the method calls.

804b in size
last modified May 6, 2006 at 9:54pm
5924 views, 4989 downloads
list all projects :: this project's forum
added Sep 19, 2002 at 3:00pm :: updated Jul 30, 2004 at 6:52am :: 2 files :: 21785 views

© Andrew Collington, 2002-2007.