Jump to content

Counting hours


Andrew R

Recommended Posts

Hi

 

How do I count up hours in php taking into consideration that there are 60 minutes in an hour and 60 seconds in a minute?

 

For example if I want to count 01:25:00 and 01:30:00 taking into consideration that there are 60 minutes in an hour.

 

If I were to count the two normally in php it would add up to, 2 but I want it to add up to 02:55:00.  How would I do that?

 

Cheers

 

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/
Share on other sites

Thanks for that.  Although in most cases I'll be counting hours from a database so I won't be able to convert the hours to seconds.  For example

 

<?php
$sql = mysql_query("SELECT SUM(online_time) AS count FROM users");
$count = mysql_result($sql, 0, 'count');
echo "$count";
?>

 

In my database all the times (online_time) are in the format of hh:mm:ss (00:00:00)

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176137
Share on other sites

No luck with that script ted_chou12.  What I basically want to do is total up all the hours in the database and echo them taking into consideration how many minutes/seconds there is in an hour.  In my database there are over 30 totals to add up.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176199
Share on other sites

can you give me an example of your ideal output?

because I dont quite understand minutes/seconds there is in an hour..

Ted

 

The output I want is something like 00:00:00 (H:i:s)

 

What I mean about the minutes/seconds is that if you normally count hours, minutes and seconds up you get the wrong output.  For example if I add up 1 hour, 30 minutes against 1 hour, 25 minutes I want the output to be 02:55:00 rather than just 255.

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176211
Share on other sites

oh! ;), that gave me the right idea! Whats your table name and the time column name please?

Ted

 

Column Name = online_time

Table Name = users

 

Hard example: What is 2:45 + 1:20 ?

 

Add the Hours: 2+1 = 3

Add the Minutes: 45+20 = 65

The minutes are 60 or more, so subtract 60 from minutes (65-60 = 5 Minutes) and add 1 to Hours (3+1 = 4 Hours) ... so the answer is 4:05

Above - Best example I can find  ;D

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176216
Share on other sites

I guess this should work, please dont hesitate to tell me if it doesnt:

<?php
$read = mysql_query("SELECT online_time FROM users");
while($r = mysql_fetch_array($read)){
$time = strtotime($r['online_time']);
$array[] = $time;
}
array_sum($array);
$time = date('H:i:s',$time);
echo $time;
?>

Ted

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176220
Share on other sites

if that one doesnt, try this one:

<?php
$read = mysql_query("SELECT online_time FROM users");
while($r = mysql_fetch_array($read)){
$time = explode(":", $r['online_time'])
$hour[] = $time[0];
$minute[] = $time[1];
$second[] = $time[2];
}
array_sum($hour);
array_sum($minute);
array_sum($second);
while ($second > 60) {$minute + 1;$second - 60;}
while ($minute > 60) {$hour + 1;$minute - 60;}
$time = "$hour:$minute:$second";
$time = date('H:i:s',$time);
echo $time;
?>

Ted

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176227
Share on other sites

what format do you store the time / date in? If you use unix timestamp e.g. 1175662800, Then I have a nice little script that converts the difference between 2 unix timestamps into readable time.

 

Simply pass the seconds into the function and it returns something like 1:30:25

<?php
function convert_seconds ($sec, $padHours = false) 
  {

    // holds formatted string
    $hms = "";
    
    // there are 3600 seconds in an hour, so if we
    // divide total seconds by 3600 and throw away
    // the remainder, we've got the number of hours
    $hours = intval(intval($sec) / 3600); 

    // add to $hms, with a leading 0 if asked for
    $hms .= ($padHours) 
          ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
          : $hours. ':';
     
    // dividing the total seconds by 60 will give us
    // the number of minutes, but we're interested in 
    // minutes past the hour: to get that, we need to 
    // divide by 60 again and keep the remainder
    $minutes = intval(($sec / 60) % 60); 

    // then add to $hms (with a leading 0 if needed)
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';

    // seconds are simple - just divide the total
    // seconds by 60 and keep the remainder
    $seconds = intval($sec % 60); 

    // add to $hms, again with a leading 0 if needed
    $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);

    // done!
    return $hms;
    
  }
?>

 

 

Hats off to the chap who wrote it.

 

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176249
Share on other sites

Here's a better way of doing this:

<?php
$tt = 0;
$midnight = strtotime(date('Y-m-d') . ' 00:00:00');
$read = mysql_query("SELECT online_time FROM users");
while($r = mysql_fetch_assoc($read))
$tt += strtotime($r['online_time']) - $midnight;
$hours = floor($tt/3600);
$x = $tt - floor($tt/3600)*3600;
$mins = floor($x/60);
$secs = $x - floor($x/60) * 60;
echo 'Total online time is ' . $hours . ':' . $mins . ':' . $secs;
?>

 

Ken

 

 

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176252
Share on other sites

Then there is something wrong with the query. Change this line:

<?php
$read = mysql_query("SELECT online_time FROM users");
?>

to

<?php
$q = "SELECT online_time FROM users";
$read = mysql_query($q) or die("Problem with the query:<pre>$q</pre><br>" . mysql_error());
?>

and see what error is printed.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/36921-counting-hours/#findComment-176364
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.