Jump to content

Recommended Posts

I have a app that i am working on and it has got me a lil fired up this am!! lol. here is what i am trying to do.

 

My Issue

I am trying to get the total number of hours worked form the db by adding the start_time and the end_time together.

 

My Code Snippet

<?
$results = mysql_query("SELECT * FROM `simp_time_logger` WHERE p_id = '$id'") or die (mysql_error());
while ($rows = mysql_fetch_array($results)) {
$explode_time = explode(" ", $rows['start_time']);
$explode_time2 = explode(" ", $rows['end_time']);
$start =  $explode_time[0];
$end =  $explode_time2[0];
$time = $start + $end;
}
echo $time;
?>

 

My Error

Says the number of hours worked is 16, How can this be when the start time is 8:29:55 and the end time is 8:29:57

 

Now the time is stored in the db like this 8:29:55 AM i am using explode to break off the AM/PM form it. I just need it to read the rows of the table and then get the correct total of hours worked to display.

 

Any help will be greatly appreciated. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/188051-total-the-hours-help/
Share on other sites

 

Convert to seconds do the math and convert back to HH:MM:SS.

 

 

<?php
function time_to_sec($time) {
    $hours = substr($time, 0, -6);
    $minutes = substr($time, -5, 2);
    $seconds = substr($time, -2);

    return $hours * 3600 + $minutes * 60 + $seconds;
}

function sec_to_time($seconds) {
    $hours = floor($seconds / 3600);
    $minutes = floor($seconds % 3600 / 60);
    $seconds = $seconds % 60;

    return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/188051-total-the-hours-help/#findComment-992818
Share on other sites

How can this be when the start time is 8:29:55 and the end time is 8:29:57

 

It is because of how PHP handles strings (http://php.net/manual/en/language.types.string.php#language.types.string.conversion)

 

When you did

 

$time = $start + $end;

 

It converted both $start and $end to an integer (because of the + operand) resulting $start in 8 and $end in 8 which make your 16 total.

 

while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) {
    $start = strtotime($row['start_time']);
    $end = strtotime($row['end_time']);
    $diff = $end - $start;
    $hours = floor($diff / 3600);
    $minutes = floor($diff % 3600 / 60);
    $seconds = $diff % 60;
    echo date('d M Y', $start), ' + ', date('H:m:s', mktime($hours, $minutes, $seconds)), ' = ', date('d M Y', $end);
}

 

Chances are this can be written a lot shorter.

Link to comment
https://forums.phpfreaks.com/topic/188051-total-the-hours-help/#findComment-992863
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.