PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 1st, 2004, 06:54 AM
bbodien bbodien is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 7 bbodien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via MSN to bbodien
Question Summing SQL time values in PHP?

Hey all,

I've got a mySQL database that has in a table a couple of columns - timeA and timeB. They take the form HH:MM:SS.

I get these out of the database into PHP, and I want to get the total time. I've tried:

PHP Code:
 $total $timeA $timeB


Evidently this doesn't work, as they aren't pure numeric values. Is there a function in PHP to add variables of this type, or will I have to explode them around ':' and then do some modulo arthimetic to manually add them?

Thanks

Reply With Quote
  #2  
Old February 1st, 2004, 07:14 PM
Mike_r Mike_r is offline
ExoCrew
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 68 Mike_r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I dont guess there is any other way than using the explode function and then doing the arithematic stuff.

Well try strtotime(), I am not sure if that will work with your time format though.
__________________
ExoHelpDesk
ExoCrew Free Services

Reply With Quote
  #3  
Old February 2nd, 2004, 05:21 AM
bbodien bbodien is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 7 bbodien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via MSN to bbodien
Yeah, I got bored and did it the long way. Here's the code if anyone else might find it useful:

(Cook time is one time value from the database, Prep time is another. This code breaks them into separate hours, mins and seconds, adds them, then converts the result back to hh:mm:ss format)

PHP Code:
// Calculate total time to prepare
    // 0=HH, 1=MM, 2=SS
    
$_prep_time explode(":"$prep_time);
    
$_cook_time explode(":"$cook_time);  

    
// Convert each time to seconds
    
$prep_seconds = (($_prep_time[0] * 3600) + ($_prep_time[1] * 60) + ($_prep_time[2]));
    
$cook_seconds = (($_cook_time[0] * 3600) + ($_cook_time[1] * 60) + ($_cook_time[2]));
    
    
// Add to produce total
    
$total $prep_seconds $cook_seconds;

    
// Convert total from seconds to full time again
    
$_total_time["hrs"] = floor(($total 3600));
    
$_total_time["mins"] = floor(($total - ($_total_time["hrs"] * 3600)) / 60);
    
$_total_time["secs"] = floor(($total - ($_total_time["hrs"] * 3600) - ($_total_time["mins"] * 60)));

    
// Prefix with 0s where necessary
    
if (strlen($_total_time["hrs"]) == 1
      
$_total_time["hrs"] = "0" $_total_time["hrs"];
    if (
strlen($_total_time["mins"]) == 1)
      
$_total_time["mins"] = "0" $_total_time["mins"];
    if (
strlen($_total_time["secs"]) == 1)
      
$_total_time["secs"] = "0" $_total_time["secs"];

    
// Format total time
    
$total_time $_total_time["hrs"] . ":" $_total_time["mins"] . ":" $_total_time["secs"]; 

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Summing SQL time values in PHP?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway