bear
bear

Reputation: 11605

Calculating time with timestamp

Sorry for the slightly misleading title, I'm not really sure how to word this.

Basically, I'm working on a time clock-in system.

I've got the data for each clock in: clock in timestamp and clock out timestamp.

After all of the clockins have been displayed in a specific period, the script adds up all of the differences between the two timestamps.

The one thing I need to do now, is actually convert this figure into hours and minutes.

Upvotes: 0

Views: 1262

Answers (2)

Navneet Pandey
Navneet Pandey

Reputation: 133

Use this function from more detailed output

function datefor($date, $time)
 {
$days = floor($time / (60 * 60 * 24));
$remainder = $time % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

if($days > 0) {
        $data = date('F d Y', $date);
        reset($date);
    }     
    elseif($days == 0 && $hours == 0 && $minutes == 0) {
        $data = "few seconds ago";
    }
    elseif($days == 0 && $hours == 0) {
        $data = $minutes.' minutes ago';
    }
    elseif($days == 0 && $hours > 0) {
        $data = $hours.' hour ago';
    }
    else {
        $data = "few seconds ago";
    }        

    return $data;
}

and select using the following statement

SELECT *,UNIX_TIMESTAMP() - datetime AS TimeSpent FROM `table_name`;

$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){ 

//call function

$check = datefor($rows['datetime'], $rows['TimeSpent']);}

now echo $check; where you want the time to be displayed.

Upvotes: 1

bear
bear

Reputation: 11605

<strong>So far, you have worked  <?php
$hours =  floor($i / 3600);
$i -= 3600 * floor($i / 3600);
$minutes  = floor($i / 60);
echo $hours; ?> hours and  <?php echo $minutes; ?> minutes</strong>

yep, it works.

Upvotes: 0

Related Questions