Forum Moderators: coopster

Message Too Old, No Replies

same time different results

whats going on?

         

knighty

4:45 pm on Jan 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the following code:

$start=mktime (10,30,0,1,1,03);
$end=mktime (10,45,0,1,1,03);
$result=$end - $start;

echo "<p> ". date("G:i", $start). " to ". date("G:i", $end). " Total Worked - ". date("G:i", $result). " hours</p>";

On my local machine it works fine i get:

10:30 to 10:45 Total Worked - 0:15 hours

but as soon as I upload it to another server i get :

10:30 to 10:45 Total Worked - 1:15 hours

Since I'm making a false time it should'nt make any difference where its hosted. right?

jatar_k

5:14 pm on Jan 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Since I'm making a false time it should'nt make any difference where its hosted. right?

right. That is very strange, you have the same code working on another machine and the logic looks sound (though I just got up so who knows).

but

I tried it on one of my servers and got this

10:30 to 10:45 Total Worked - 19:15 hours

obviously the logic is somehow skewed. hmmmm.

jatar_k

6:12 pm on Jan 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What about something like this?

$start=mktime (10,30,0,1,1,03);
$end=mktime (10,45,0,1,1,03);
$result=$end - $start;
$dhour = $result/3600; // to get hours
$diff = split ('[.]', $dhour);
echo "<p> ". date("G:i", $start). " to ". date("G:i", $end);
echo "<br>Total Worked is " . $diff[0] . " hours and " . ($diff[1]/100)*60 . " minutes.";

<added>the logic, I figured, is that we just need the difference between the two, which we get in seconds. From there you can do the math by hand instead of trying to get the date() function to do it. Especially since the date() function doesn't seem to be up to it.

knighty

9:14 am on Jan 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k,

That works fine although I had to tweak the code to get 30 mins to appear - otherwise you just get a 3.

Still not sure why I get the results I do the other way.

Since I'm going to be storing $start and $end and wanting to calculate totals over weeks and months maybe I would be better off not using mktime at all and just converting basic numbers like 10:30 to seconds and minutes?

jatar_k

6:03 pm on Jan 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mktime is probably fine, one line and it gives you seconds. I think converting it will be longer but maybe not.

What was your little tweak? (just interested :))

knighty

9:10 am on Jan 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




The code you gave works fine but if you get a result like 1:30 mins it actually comes out 1:3

so i just whacked in:

if (strlen($mins)<2) { $mins=$mins*10;}

(not a perfect solution but I only use increments of 15 minutes)

knighty

3:26 pm on Jan 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bling blang,

$x=mktime (0,10,1,1,1,03);
$y=mktime (0,10,1,1,1,03);
$z=mktime (0,10,1,1,1,03);
$xyz=$x + $y;
$xyz=date("G:i", $xyz);
echo "total = $xyz";

just adding x + Y give the correct answer of 20 mins BUT throw in Z and it all goes pear shaped

why? I have searched everywhere trying to find an answer...someone please tell me why *sob* *sob*