GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-May-2005, 06:57
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 834
admin will become famous soon enough

Re: Get the date / time difference with PHP


I just posted a GID Community blog entitled: Get the date / time difference with PHP.

Quote:
How do I calculate the difference between 2 time or date values with PHP? This was a question posed by justinhn, a member of GIDForums, today.

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.... more.

Please let me know what you think.

This is an auto-generated message posted by GIDNetwork™ on behalf of the author.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #2  
Old 14-Jun-2006, 02:41
debembie debembie is offline
New Member
 
Join Date: Jun 2006
Posts: 2
debembie is on a distinguished road

calculating time difference between two date


Hi all,
i'm new here, i have problem with calculating time difference , i've read the thread "Time Calculation not considering minutes" by justinhn, and i found the answer in "Get the date / time difference with PHP" by admin, so i've tried that code and it works!, but when i tried to input:
example: START time value = 23:30 and END time value = 24:00, the result was error,and then i change END time to= 00:00 and the result still error, i didn't understand where are the problems,and why it could happen......:?: ,
and the main problems:!: : i want to ask about how to calculating time difference in HH:MM:SS (hours:minutes:seconds) between two different date,for the example, the input are :
$start_date : 2006-06-08, $start_time : 23:30
$end_date : 2006-06-09, $end_time : 02:15,

Please help me to solve this problem, because i can't find any answer from any search engine like google or yahoo.

THANKS after and before.:smile:
  #3  
Old 14-Jun-2006, 08:37
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: calculating time difference between two date


Thank you for your question.

If you ask me how many hours between 03:00:00 and 00:00:00, I can immediately assume you mean midnight (00:00:00) of tomorrow. But a computer doesn't assume, so if you submit just the HH:MM:SS bits to the function, it assumes it's midnight of today and not midnight of tomorrow (which is what I assume you meant). Get it?

If you look at the insides of the function, you will see that the unixtimestamp is set by this line:

PHP Code:

//...

    $uts['start']      =    strtotime( $start ); // ref. to http://www.gnu.org/software/tar/manual/html_node/tar_110.html#SEC110 for valid formats
    $uts['end']        =    strtotime( $end ); 



It's always safe to include the date values too... for example, using your example values...

PHP Code:

// what is the time difference between $end and $start?
if( $diff=@get_time_difference("$start_date $start_time", "$end_date $end_time") ) // <-- HERE!
{
  echo "Hours: " .
       sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] );
}
else
{
  echo "Hours: Error";
} 


  #4  
Old 14-Jun-2006, 22:47
debembie debembie is offline
New Member
 
Join Date: Jun 2006
Posts: 2
debembie is on a distinguished road

Re: Get the date / time difference with PHP


Thanks alot for the answer, i really appreciated that.
i've tried the code and it's work, but we've another problem here, when we have input like this :

$start_date = '2006-06-08';
$start_time = '22:30';
$end_date = '2006-06-09';
$end_time = '22:30';
why the result are(in HH:MM) : 00:00 hours ? not 24 (hours):00 (minutes)?

or....

//for example, it have different about 2 days
$start_date = '2006-06-08';
$start_time = '22:30';
$end_date = '2006-06-10';
$end_time = '10:30';

why the result are(in HH:MM) : 12:00 hours ?, not 36 (hours): 00 (minutes)
how can we get total time, is it possible to calculating them?

Thanks.
  #5  
Old 16-Nov-2006, 00:43
rockyford1000 rockyford1000 is offline
New Member
 
Join Date: Nov 2006
Posts: 3
rockyford1000 is on a distinguished road

Re: Get the date / time difference with PHP


hey..in relation..

i have also problem in tracing the time difference

my start date is=08.1.2006
my end date is=12.1.2006
how will i able to know that the if i already expire or not..in short the date between 08.1.2006 and 12.1.2006


please im new here i need some help..
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Limit combo box and date time picker choice according to database created in folder shinyhui C++ Forum 0 22-Feb-2005 21:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 03:13
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
Easily keep up to date with the latest PHP news (and any other for that matter) jrobbio MySQL / PHP Forum 0 03-Jun-2003 04:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:17.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.