GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-May-2005, 17:48
justinhn justinhn is offline
New Member
 
Join Date: May 2005
Posts: 3
justinhn is on a distinguished road

Time Calculation not considering minutes


Hey guys,

I'm having a little trouble with time manipulation using PHP, was wondering if anybody could help me.

I'm trying to input 2 time strings, and then find their difference.
eg $start==09:00, $finish==10:30,
and get $difference==01:30

My problem is that currently I am able to subtracts hours, but the minutes are not being considered. So the result i get from the above is 01:00 not 01:30. If i had inputted start=9:00, finish=10:59, i'd still get difference=1:00.

Here is my code so far:

PHP Code:

$start == 09:00;
$finish ==10:30;
$startConvert == date("H:i", strtotime("".$start));
$finishConvert == date("H:i", strtotime("".$finish));
$difference =  $finishConvert - $startConvert;
$differenceConvert = date("H:i", strtotime("".$difference)); 



Any suggestions would be most appreciated

- Justin
Last edited by JdS : 31-May-2005 at 03:55. Reason: please insert your example PHP codes between [php] and [/php] bbcode tags
  #2  
Old 31-May-2005, 04:07
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

calculating time/date difference


I understand your question and I immediately searched Google because I was almost certain someone would have such a (useful) PHP function written already.

Quickly going through the links off the first 2 (SERPS) pages off a Google Search for "get time difference +php", yielded nothing like what I was expecting!

I am in the mood so I think I will write a little custom function to do this easily. I will reply here when I am done...
  #3  
Old 31-May-2005, 06:08
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
OK, here's the function I just wrote: Get the date / time difference with PHP. I will appreciate any feedback you can share.

If you need me to explain anything, do not hesitate to ask.
  #4  
Old 01-Nov-2006, 03:25
janakiramant janakiramant is offline
New Member
 
Join Date: Nov 2006
Posts: 1
janakiramant is on a distinguished road

Re: Time (difference) Calculation not considering minutes


HI,

Thanks for the coding. Its great. I have a problem in this. I Have a column named timeinterval which is a datetime field. I need to display every record which is greater than 30 seconds. say i have a record 2006-10-31 11:50:01. I will display this record first as i have ordered by timeinterval and asc. I will take the second record(2006-10-31 11:50:31) compare it with this one. If it is greater than 30 seconds i will display this record else proceed with the next record.

How to achieve this. I tried using datediff and timediff. But both doesnt serve the purpose.

I just incorporated ur code but it says error. Ending date/time is earlier than the start date/time. Will ur function solve my problem. If so how to handle it. Iam getting error. The end date is greater than the start date.

Thanks
jankiraman
  #5  
Old 04-Dec-2007, 00:58
jagpreet jagpreet is offline
New Member
 
Join Date: Dec 2007
Posts: 1
jagpreet is on a distinguished road

Re: Time Calculation not considering minutes


Hey Guys,

Here is a solution for it ....just use it and enjoy

PHP Code:

##################################
$start = "09:00";
$finish = "10:30";
 
 
$tempstart=explode(":",$start);
$tempEnd=explode(":",$finish);
$tt=(($tempstart[0]*60)+$tempstart[1])+2400; 
$pp=(($tempEnd[0]*60)+$tempEnd[1]);
$difference=$tt-$pp;
$difference=$difference%2400;
$difftime=intval($difference/60).":".intval($difference%60);
################################### 




Jagpreet
Globtier Infotech


Quote:
Originally Posted by justinhn
Hey guys,

I'm having a little trouble with time manipulation using PHP, was wondering if anybody could help me.

I'm trying to input 2 time strings, and then find their difference.
eg $start==09:00, $finish==10:30,
and get $difference==01:30

My problem is that currently I am able to subtracts hours, but the minutes are not being considered. So the result i get from the above is 01:00 not 01:30. If i had inputted start=9:00, finish=10:59, i'd still get difference=1:00.

Here is my code so far:

PHP Code:

$start == 09:00;
$finish ==10:30;
$startConvert == date("H:i", strtotime("".$start));
$finishConvert == date("H:i", strtotime("".$finish));
$difference = $finishConvert - $startConvert;
$differenceConvert = date("H:i", strtotime("".$difference)); 



Any suggestions would be most appreciated

- Justin
Last edited by admin II : 05-Dec-2007 at 08:44. Reason: Please surround your PHP code with [php] your code [/php]
 


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 On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulation Problem wu_weidong C++ Forum 7 12-Mar-2005 22:56
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:39.


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