SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Better way to calculate time?

    I use the following code to turn seconds into hours:minutes:seconds

    Code:
    while($res = mysql_fetch_array($this->db->select_record()))
    		{
    			$duration = $duration + $res['duration'];
    		}
    $minutes = (int)($duration / 60);
    		$seconds = $duration - ($minutes * 60);
    		$hours = (int)($minutes / 60);
    		$minutes = $minutes - ($hours * 60);
    		
    		if(strlen($seconds) == 1)
    			$seconds = "0".$seconds;
    			
    		if(strlen($minutes) == 1)
    			$minutes = "0".$minutes;
    			
    		if($hours != '' && $hours > 0)
    			$time = $hours.":";
    			
    		$time .= $minutes.":".$seconds;
    		
    		return $time;
    This loading on one page used up all the CPU cycles. It only pulled 4 results in this query: 4343, 5025, 1941, 411

    There doesn't seem to be anything excessive to me that should have caused it to overload the server so much. Is there a better way to do this?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, that's a killer. Use sum() in your sql query to calculate total duration rather than fetching records one by one.
    Saul

  3. #3
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is that what's causing it?

    So I would change it to
    SELECT sum(duration) FROM blocks WHERE id IN ('1')

    Like that?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jhorra View Post
    Is that what's causing it?

    So I would change it to
    SELECT sum(duration) FROM blocks WHERE id IN ('1')

    Like that?
    That's definitely causing that and yes, try this.
    Saul

  5. #5
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it's just the duration of a video clip in seconds.

  6. #6
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, you're right, it flies right through it now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •