Jump to content

php time calculation


arjdat

Recommended Posts

i have a db table with four columns: adin, edin, out and diff

first three columns' values are inserted manually.

i want the diff column to show difference between out time and maximum of edin time and adin time and insert the record into diff column in my phpmyadmin.
Link to comment
Share on other sites

columns are in datetime format; i did most of job in dreamweaver including the connection and form to insert values and showing the result in table by dynamic table; so the codes are not written by me but by dreamweaver itself.

Link to comment
Share on other sites

ED IN AD IN OUT DIFF 12:20:00 12:10:00 12:23:00 00:00:00

 

when i use integer format, diff column gives exact result i.e. out-max(edin, adin); when i want them in time format it does not work; like in the example given above, diff should be 00:03:00 or even better 00:03 (omitting the second part and giving only hr; mm). below is the code i use for getting the value and put into diff column

 

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO chk (edin, adin, `out`, diff) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['edin'], "date"),
                       GetSQLValueString($_POST['adin'], "date"),
                       GetSQLValueString($_POST['out'], "date"),
  GetSQLValueString($_POST['out'] - max($_POST['adin'], $_POST['edin']), "date"));
Link to comment
Share on other sites

Firstly you should use MySQL TIME type for your 'diff' field. It would be even better if you could use the DATETIME format for the other 3 fields and include the date in the data. If you are using a VARCHAR then change it.

 

To insert the missing data using the INSERT query you must first use PHP to calculate the time difference. To do this you will need the date. There is an issue. If the start time is say 5pm and the end time is 2am the following morning you will need the actual end date as just adding the current day to your time values will not work. So, $_POST['edin'] must be a datetime and $_POST['out'] must be a datetime YYYY-MM-DD HH::MM::SS

 

It is then easy to find the time difference using a simple function. If you are using PHP 5.3 then there are objects that can do this much easier.

<?php
// function get the time difference between start and end date
function get_time_diff($start, $end)
{
 $diff = strtotime($end) - strtotime($start);
 $hr = floor($diff / 3600);
 $remaining = $diff - $hr * 3600;
 return sprintf('%02d', $hr) . gmdate(':i:s', $remaining);
}

// this should be what is in your $_POST['edin'] variable
$start = '2013-07-01 12:20:00';
// this should be what is in your $_POST['out'] variable
$end = '2013-07-01 12:23:00';
// should print 00:03:00
$out = get_time_diff($start, $end);
echo $out;
?>
Edited by neil.johnson
Link to comment
Share on other sites

Why are you storing a calculation in the DB? You should store the values that are used in the calculation and then calculate the value when you need to use it.

 

Not really. What if you wanted to pull a report on all people who worked longer than 5 hours?

Link to comment
Share on other sites

Not really. What if you wanted to pull a report on all people who worked longer than 5 hours?

Yes really.  That's basic database.  You run the calculation in the query: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

 

Also, don't use math to calculate date/time differences, use the time change aware functions or datetime class.  Your get_time_diff() example fails if the dates on are on opposite sides of a daylight savings time change.

Link to comment
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.