| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Regular Join Date: Dec 2005 Location: India
Posts: 274
![]() ![]() | (php) 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 |
| |
| | #2 (permalink) |
| Senior Member Join Date: Jan 2007 Location: Wales - United Kingdom
Posts: 1,774
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | This isn't exactly working.. not too sure why. Although the basic concept behind this is: 1) Convert both times to their UNIX timestamp 2) Add together 3) Output as a time. I came up with this: Code: <?php
$c1 = "29:00:00";
$c2 = "05:10:00";
$Newc1 = mktime($c1);
$Newc2 = mktime($c2);
$NewTime = $Newc1 + $Newc2;
echo date ('G:i:s', $NewTime);
?> |
| |
| | #4 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,558
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Actually Tristanperry is on the righ track but there are several arguments for the mktime function: http://php.net/mktime Basically you need to split your string and extract hour, minute, second and pass these arguments to mktime
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists |
| |
| | #6 (permalink) |
| Senior Member Join Date: Jan 2007 Location: Wales - United Kingdom
Posts: 1,774
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Ah good point.. so the string needs to be exploded.. Code: <?php
$c1 = "29:00:00";
$c1 = explode(':', $c1);
$c2 = "05:10:00";
$c2 = explode(':', $c2);
$Newc1 = mktime($c1[0],$c1[1],$c1[2]);
$Newc2 = mktime($c2[0],$c2[1],$c2[2]);
$NewTime = $Newc1 + $Newc2;
echo date ('G:i:s', $NewTime);
?> ????: NamePros.com http://www.namepros.com/programming/332168-php-help-needed-addition-two-variables.html 29:00:00 - is that Hours : Minutes : Seconds Like a stop-watch or something? What are you intending to actually get by adding 29:00:00 to 05:10:00?
Last edited by tristanperry; 05-26-2007 at 06:05 AM.
|
| |
| | #7 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | As tristanperry asked is that days, hours and minutes or maybe hours, minutes and seconds it could also be minutes, seconds and milliseconds. It may sound pedantic but it is vital information if you want some help. For example if it is the 1st option then the following is the maximum possible:- 364:23:59 with the second the following shows the maximum digits:- 23:59:59 and in the 3rd:- 59:59:999 of course the 1st digit in each may not make a difference but would of course depend on your usage. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |