• Welcome to NamesLot.com Domain Name Forum

    NamesLot.com Domain Name Forum offers a fully open forum to discuss domain industry news and a 0% commission marketplace for you to buy and sell domain names.

    We have reorganized our Marketplace so now it is easier to get attention to your domain while it is also easier for buyers to find the right domain name.

    If you want us to add more threads to our marketplace, please contact us! Listing on our marketplace as always, 100% free! Register NOW or Login HERE!

Help Needed for addition of two variables Urgent...

Status
Not open for further replies.

raju79

New Member
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
 
<?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.
 
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.
 
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:
<?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.
 
Thanks for suggestion slightly modify.

PHP:
{
$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
 
Status
Not open for further replies.

Members online

No members online now.

Forum statistics

Threads
19,801
Messages
69,798
Members
44,585
Latest member
tsscgroup
Active members today
0
New members today
0
New threads today
2
New posts today
2

Follow NamesLot on Twitter!

NamesLot proudly supported by

NamesLot proudly supported by

Top