Jump to content

Recommended Posts

Can you define the value of a field to automaticaly perform a math function between two other fields?

 

I have the following fields in my table, OnSiteTime, OffSiteTime, and TotalTime.

 

Can I define TotalTime to automatically subtract OnSiteTime from OffSiteTime in the field definition somewhere?

Link to comment
https://forums.phpfreaks.com/topic/134041-math-functions-inside-mysql/
Share on other sites

Apparently YOU CAN NOT do this. According to the phpadmin help section.  So perhaps someone can help me with the code then. 

 

As you can see, I want to calculate the amount of time on each jobsite, based on the arrival and departure times.  But I am having a bit of a problem with the time formatting.  A lot of the work will be done during the eveing, so I will need to be able to figure the time spend at a job when the clock switches from 11:59 pm to 12:00 am. 

 

Example:  11:00 pm - 11:30 pm = 30min TotalTime

but....    11:45 pm - 12:15 am = screwed up number.

 

how can I do this with php?

Heres a small code I ran across that sort of works.  Two problems.  One is I would like to use the 24hr military time settings if possible, since that is how my original form asks for it. If not, thats fine, ill change the form. Second problem is, getting this code to realize the transformation from 11 at night to 2 the next morning. It thinks there is 9hour of difference. Any suggestions?

 

<? $start_time = $_REQUEST['start_time']; $end_time = $_REQUEST['end_time']; $action = $_REQUEST['action']; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<body> 
<h3>Parse Times</h3> 
<form action="<?= $_SERVER['PHP_SELF'] ?>"> Enter the start time (hours:minutes) 
<input type="text" name="start_time" value="<?= $start_time ?>" size="60"> <br/>  Enter the end time (hours:minutes) 
<input type="text" name="end_time" value="<?= $end_time ?>" size="60"> <br/> 
<input type="submit" value="Go!"> <br/>

<input type="hidden" name="action" value="go"> </form> <hr> 

<? if($action && ($action == "go")){
     list($hours, $minutes) = split(':', $start_time);     $startTimestamp = mktime($hours, $minutes); 
     list($hours, $minutes) = split(':', $end_time);     $endTimestamp = mktime($hours, $minutes);  
 $seconds = $endTimestamp - $startTimestamp;     
 $minutes = ($seconds / 60) % 60;     
 $hours = round($seconds / (60 * 60)); 

echo "Time On Job Site: <b>$hours</b> hours and <b>$minutes</b> minutes"; } ?> 

</body> 
</html>

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.