Hey, Would you like to work at Home ?? Just click here No need to pay, just register free and activate your account and get data Entry Work at your Home.

Friday, October 19, 2007

Get the date / time difference with PHP






Get the date / time difference with PHP




How do I calculate the difference between 2 time or date values with PHP?



It seems like a really popular question, I was so sure there would already be something ready-made out there. Searching the WWW with my favourite search engine, I was a bit surprised that it was clearly not the case -- okay, there were some example codes that do calculate the difference in time values, but they were not quite what I was hoping to find.

Before I paste my custom PHP function to calculate time differences, I need to explain that I have no use for this function myself. A disclaimer like that usually means I am writing it, testing it for a while and then I am done with it. In case you find a bug, I will appreciate it if you let me know, either via the comment link at the bottom of this page or by sending me an email.

Overview: get_time_difference()

array get_time_difference( string start, string end )

The function expects to be given 2 strings representing the start and end values of a time or date. These strings will be converted to Unix timestamps before the function works with them. Unix timestamp is the number of seconds since January 1, 1970 00:00:00 GMT.

The function returns an array that can be described like this:

Generic Code Example:



$diff['days'] = int
$diff['hours'] = int
$diff['minutes'] = int
$diff['seconds'] = int

or returns false on errors.

The custom PHP function: get_time_difference

PHP Code Example:

<?php
/**
 * Function to calculate date or time difference.

 * 
 * Function to calculate date or time difference. Returns an array or
 * false on error.

 *
 * @author       J de Silva                             <giddomains@gmail.com>
 * @copyright    Copyright &copy; 2005, J de Silva
 * @link         http://www.gidnetwork.com/b-16.html    Get the date / time difference with PHP

 * @param        string                                 $start
 * @param        string                                 $end
 * @return       array
 */
function get_time_difference$start$end )
{

    
$uts['start']      =    strtotime$start );
    
$uts['end']        =    strtotime$end );

    if( 
$uts['start']!==-&& $uts['end']!==-)
    {
        if( 
$uts['end'] >= $uts['start'] )

        {
            
$diff    =    $uts['end'] - $uts['start'];
            if( 
$days=intval((floor($diff/86400))) )

                
$diff $diff 86400;
            if( 
$hours=intval((floor($diff/3600))) )

                
$diff $diff 3600;
            if( 
$minutes=intval((floor($diff/60))) )

                
$diff $diff 60;
            
$diff    =    intval$diff );            
            return( array(
'days'=>$days'hours'=>$hours'minutes'=>$minutes'seconds'=>$diff) );

        }
        else
        {
            
trigger_error"Ending date/time is earlier than the start date/time"E_USER_WARNING );
        }

    }
    else
    {
        
trigger_error"Invalid date/time data detected"E_USER_WARNING );
    }
    return( 
false );
}
?>





Example PHP script using "get_time_difference"

Using justinhn's example, here is how you could use this function to get the time difference.

PHP Code Example:

<?php

// a START time value
$start '09:00';
// an END time value
$end   '10:30';


// what is the time difference between $end and $start?
if( $diff=@get_time_difference($start$end) )
{

  echo 
"Hours: " .
       
sprintf'%02d:%02d'$diff['hours'], $diff['minutes'] );
}
else
{

  echo 
"Hours: Error";
}

?>

The example code will result in this being output:

Generic Code Example:


Hours: 01:30

1 comment:

Pollab said...

Nice Code, Works fine with out any problem. Thanks.

Your Ad Here