 |
|
10-06-2009, 06:34 AM
|
|
#1
|
 |
|
Aspirant (Level 2)
Join Date: Apr 2009
Posts: 19
|
[SOLVED]Finding elapsed time between two times
Hello.
I'm having an issue with calculating the elapsed time between two times, and since my hoster doesn't provide PHP 3.0 yet, I can't make the premade functions, so I have to make my own.
Now, my PHP knowledge is new so I'm not used to functions and don't know many.
Code:
function time_diff($timeDeltaOne, $timeDeltaTwo){
if($timeDeltaOne > $timeDeltaTwo){
$time_latter = $timeDeltaOne;
$time_earlier = $timeDeltaTwo;
}
else{
$time_earlier = $timeDeltaOne;
$time_latter = $timeDeltaTwo;
}
$diff = time_latter - time_earlier;
return $diff;
}
This doesn't work, which is kinda obvious, I need integers but I don't know how to get some :S
So all help is appreciated and thanks in advance!
Last edited by Akkernight : 10-10-2009 at 02:14 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-06-2009, 07:19 PM
|
|
#2
|
 |
|
Mod of the Underlay
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
|
Code:
$diff = $time_latter - $time_earlier;
not at all sure why your function doesn't work (if the above is a typo) - what values are you sending to the function?
Can we see an example of use?
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak
The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Last edited by Horus_Kol : 10-07-2009 at 09:52 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-07-2009, 09:32 AM
|
|
#3
|
 |
|
Aspirant (Level 2)
Join Date: Apr 2009
Posts: 19
|
oh whoops! I forgot the values, I was poundering over what code I was missing to post :S
Code:
$date = new DateTime();
$lastDate = new DateTime();
time_diff($date, $lateDate);
I hope this helps with the confusion
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-07-2009, 12:36 PM
|
|
#4
|
 |
|
Blonde Bimbo
Join Date: Jul 2004
Posts: 2,244
|
Quote:
Originally Posted by Akkernight
and since my hoster doesn't provide PHP 3.0
|
Change hosts!
Quote:
Originally Posted by Horus_Kol
Code:
$diff = $time_latter - $time_earlier;
|
Assume you have the red bits in your code?
Quote:
Originally Posted by Akkernight
Code:
$date = new DateTime();
$lastDate = new DateTime();
time_diff($date, $lateDate);
|
$la stDate && $la teDate?
Also, you don't do anything with the result of your function?
¥
__________________
I may have opened the door, but you entered of your own free will
Last edited by ¥åßßå : 10-07-2009 at 12:38 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-07-2009, 01:00 PM
|
|
#5
|
 |
|
Aspirant (Level 2)
Join Date: Apr 2009
Posts: 19
|
Quote:
Originally Posted by ¥åßßå
Change hosts!
Assume you have the red bits in your code?
$lastDate && $lateDate?
Also, you don't do anything with the result of your function?
¥
|
Well, I did echo the result and it turned to be 0 ... Also, I wrote some of the code directly into the reply box, so there I made some typos :S Anyways, your time is appreciated also maybe I forgot the '$' at the sub. part...
Also, I spent 1 hour talking to my host trying to get them to update, yet eventually I just got told that it won't come for some months! And I'll have to wait 'til my subscribion's off 'til I change host
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-07-2009, 01:47 PM
|
|
#6
|
 |
|
Blonde Bimbo
Join Date: Jul 2004
Posts: 2,244
|
The chances are that your (test) times have bugger all difference ( hence the "zero" ) .. you're setting both vars within nano-seconds of each other
¥
__________________
I may have opened the door, but you entered of your own free will
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-09-2009, 05:48 PM
|
|
#9
|
 |
|
Blonde Bimbo
Join Date: Jul 2004
Posts: 2,244
|
Sorry, I've had a couple of busy days, so not even had time to think about my own problems, let alone yours .... *if* I get chance before I sod off on holiday ( for 14 days in malta  ) then I'll post it here?
In the mean time, you may wish to change your date format into a timestamp
¥
__________________
I may have opened the door, but you entered of your own free will
|
|
Add to del.icio.us
Can you digg it?
|
|
|
10-10-2009, 08:13 AM
|
|
#10
|
 |
|
Aspirant (Level 2)
Join Date: Apr 2009
Posts: 19
|
ok, fixed it 
Here's the fix.
EDIT: Feel kinda stupid for not figuring this out >.<
Code:
function DateTime_diff($DateTime_DeltaOne, $DateTime_DeltaTwo, $format){
if(strtotime($DateTime_DeltaOne->format($format)) > strtotime($DateTime_DeltaTwo->format($format))){
$time_earlier = strtotime($DateTime_DeltaTwo->format($format));
$time_latter = strtotime($DateTime_DeltaOne->format($format));
}
else{
$time_earlier = strtotime($DateTime_DeltaOne->format($format));
$time_latter = strtotime($DateTime_DeltaTwo->format($format));
}
$diff = $time_latter - $time_earlier;
return $diff;
}
Code:
$diff = DateTime_diff($lastDate, $date, "Y-m-d H:i:s");
|
|
Add to del.icio.us
Can you digg it?
|
|
 |
|
KEEP TABS |
|
SPONSORS |
| |
|
| |
|
|
| |
|