Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2004, 03:56 PM   PM User | #1
requestcode
Regular Coder

 
Join Date: Jun 2002
Posts: 626
Thanks: 0
Thanked 0 Times in 0 Posts
requestcode is an unknown quantity at this point
difference between start date and time, ending date and time

What would be the best way to calculate the difference between a start date and time and an ending date and time. Would like to display the number of days, hour, minutes and seconds. Any code examples or links to articles and tutorials would be appreciated. Thank you.
requestcode is offline   Reply With Quote
Old 01-16-2004, 04:20 PM   PM User | #2
DsgnrsTLZAdmin
Regular Coder

 
Join Date: Jan 2004
Location: Georgia
Posts: 306
Thanks: 0
Thanked 0 Times in 0 Posts
DsgnrsTLZAdmin is an unknown quantity at this point
would these times be a constant or virtual time
__________________
~Designer's Toolz~
DsgnrsTLZAdmin is offline   Reply With Quote
Old 01-16-2004, 04:26 PM   PM User | #3
requestcode
Regular Coder

 
Join Date: Jun 2002
Posts: 626
Thanks: 0
Thanked 0 Times in 0 Posts
requestcode is an unknown quantity at this point
They come from a MySql database file that is updated by a Help Desk application. Among other things it stores date and time a support request was created and date and time it was completed. The format for the date is YYYYMMDD and time is HHMMSS. The "created" date is separate from the "created" time and the same for completed. Hope this gives a better explanation. If you could point me to some examples or tutorials that would be great. Thank you.
requestcode is offline   Reply With Quote
Old 01-16-2004, 04:51 PM   PM User | #4
requestcode
Regular Coder

 
Join Date: Jun 2002
Posts: 626
Thanks: 0
Thanked 0 Times in 0 Posts
requestcode is an unknown quantity at this point
I did find this one over at the fourms at PHP Builder. I should be able to incorporate it into my existing code to do what I want. Thank you for your help.
PHP Code:
<?php
function  date_diff($first_date,$first_time,$second_date,$second_time)
{
    
// Author: Tone.
    // Date    : 15-12-2003.

    // Ref: Dates go in "12 31 2003".
    // Ref: Times go in "12 59 13".
    // Ref: mktime(HOUR,MIN,SEC,MONTH,DAY,YEAR).

    // Splits the dates into parts, to be reformatted for mktime.
    
$first_date_ex explode(" ",$first_date);
    
$first_time_ex explode(" ",$first_time);
    
$second_date_ex explode(" ",$second_date);
    
$second_time_ex explode(" ",$second_time);

    
// makes the dates and times into unix timestamps.
  
    
$first_unix  =  mktime($first_time_ex[0],$first_time_ex[1],$first_time_ex[2],$first_date_ex[0],$first_date_ex[1],$first_date_ex[2]);
    
$second_unix  =  mktime($second_time_ex[0],$second_time_ex[1],$second_time_ex[2],$second_date_ex[0],$second_date_ex[1],$second_date_ex[2]);

    
// Gets the difference between the two unix timestamps.
    
$timediff $first_unix-$second_unix;

    
// Works out the days, hours, mins and secs.
    
$days=intval($timediff/86400);
    
$remain=$timediff%86400;
    
$hours=intval($remain/3600);
    
$remain=$remain%3600;
    
$mins=intval($remain/60);
    
$secs=$remain%60;

    
// Returns a pre-formatted string. Can be chagned to an array.
    
return $days." days, ".$hours." hours, ".$mins." mins, ".$secs." secs.";
}
echo 
date_diff'01 16 2004','09 10 00','01 16 2004','09 11 00' ) ;
?>

Last edited by requestcode; 01-16-2004 at 05:00 PM..
requestcode is offline   Reply With Quote
Old 01-16-2004, 06:59 PM   PM User | #5
DsgnrsTLZAdmin
Regular Coder

 
Join Date: Jan 2004
Location: Georgia
Posts: 306
Thanks: 0
Thanked 0 Times in 0 Posts
DsgnrsTLZAdmin is an unknown quantity at this point
looks good to me, good luck
__________________
~Designer's Toolz~
DsgnrsTLZAdmin is offline   Reply With Quote
Old 01-16-2004, 11:00 PM   PM User | #6
ReadMe.txt
Regular Coder

 
Join Date: Jun 2002
Location: Sheffield, UK
Posts: 552
Thanks: 0
Thanked 0 Times in 0 Posts
ReadMe.txt is an unknown quantity at this point
if both dates come from the MySQL database then you should be able to use MySQL's built in functions for dat and time processing:

http://www.mysql.com/doc/en/Date_and...functions.html
__________________
"To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

(Me Me Me Me Me Me Me Me Me)
ReadMe.txt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:32 AM.


Advertisement
Log in to turn off these ads.