Jump to content

Math functions inside MYSQL


perficut

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>

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.