Post Reply  Post Thread 

Help Needed for addition of two variables Urgent...
Author Message
raju79
Junior Member



Posts: 3
Group: Registered
   
Cash: 0.00 Donate
Help Needed for addition of two variables Urgent...

I need to sum two variable each of them have time in time format how can i add them to have result in the same time format.

$c1 = 29:00:00
$c2 = 05:10:00


$c = $c1 + $c2;

when i echo $c it gives result as 34
But i need result as 34:10:00

Please help me out.


Thanks

05-26-2007 08:19 PM
Send the author a private message Find all posts by this author Quote this message in a reply

Advertisements

This is an ad revenue sharing forum
jignesh
Moderator

Moderators

Posts: 363
Group: Moderators
   
Cash: 307.00 Donate
RE: Help Needed for addition of two variables Urgent...

<?php
$c1 = mktime(06,00,00,0,0,0);
$c2 = mktime(05,10,00,0,0,0);
$c= $c1+$c2;
echo "Total is ".date("h:i:s", $c);
?>

use this script it will work fine.


Technical Information | Technical World | Seo Services india | BizComSoft
05-26-2007 09:27 PM
Send the author an email Send the author a private message Find all posts by this author Quote this message in a reply
raju79
Junior Member



Posts: 3
Group: Registered
   
Cash: 0.00 Donate
RE: Help Needed for addition of two variables Urgent...

It is not working. Please tyr it out with my given variable value. Time sum is coming wrong.

05-26-2007 09:37 PM
Send the author a private message Find all posts by this author Quote this message in a reply
jignesh
Moderator

Moderators

Posts: 363
Group: Moderators
   
Cash: 307.00 Donate
RE: Help Needed for addition of two variables Urgent...

see if u want it in time format that it will not display more than this value 12:59:59 so when u add two values like 10:10:45 + 1:45:45 it will give result like 10:56:30
just try it,it will work like that.

if u use this echo "Total is ".date("H:i:s", $c);
than u can get result up to 24 hours.


Technical Information | Technical World | Seo Services india | BizComSoft
05-26-2007 09:52 PM
Send the author an email Send the author a private message Find all posts by this author Quote this message in a reply
Nameslot
Administrator

Administrators

Posts: 1,503
Group: Administrators
   
Cash: 700.50 Donate
RE: Help Needed for addition of two variables Urgent...

I think the issue is not with the current problem. I think this same problem can also be solved with other simple functions. But anyway when I was new I was also getting into this kind of mess now and then. The above variables are time format that we assume but php will consider it as a string.

Back to the topic. The problem is simple here even though this is not the proper way of programming but as you have asked it I have written the code for it in simple way. First of all you know that there are only 3 possible figures in the formatted string. In there lies the solution.

Example Code:

PHP Code:

<?php
$c1 
"29:00:00";
$c2 "05:10:00";
$temp explode(":"$c1);
$temp1 explode(":"$c2);
$t0 $temp[0] + $temp1[0];
$t1 $temp[1] + $temp1[1];
$t2 $temp[2] + $temp1[2];
$c sprintf("%02d:%02d:%02d"$t0$t1$t2);
echo 
$c;
?>



Still this is not a proper way of programming but will solve your issue for now.


Nameslot.com | Do you have a question? shoot me a PM.
05-26-2007 10:53 PM
Send the author an email Send the author a private message Visit authors website Find all posts by this author Quote this message in a reply
raju79
Junior Member



Posts: 3
Group: Registered
   
Cash: 0.00 Donate
RE:  Help Needed for addition of two variables Urgent...

Thanks for suggestion slightly modify.

PHP Code:

{
$temp explode(":"$c1t);
$temp1 explode(":"$r6[0]);
$t0 $temp[0] + $temp1[0];
$t1 $temp[1] + $temp1[1];
while( 
$t1 >= 60)
{
$t0 $t0 +1;
$t1 $t1 60;
}
$t2 $temp[2] + $temp1[2];
$c1t sprintf("%02d:%02d:%02d"$t0$t1$t2);
}




Thanks again
raju

05-27-2007 02:50 AM
Send the author a private message Find all posts by this author Quote this message in a reply

Post Reply  Post Thread 


View a Printable Version | Send this Thread to a Friend | Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:
Forum Jump: