Jump to content

Need Help: Checking date field to see if an item is close to needing calibration


zoominomad
Go to solution Solved by Psycho,

Recommended Posts

I'm trying to teach myself SQL/PHP by building a database driven local site for keeping track of my calibrated tools (lot of them).

 

Basically my table looks like this:

 

Table: calibrated_items

Fields: serial_number, part_number, location, nomenclature, calib_freq, date_calib

 

These tools have different intervals at which they need to be calibrated (180 days, 360 days, etc). I'm trying to figure out how I can query my list of tools and show how many days they have until due calibration. The date_calib field is a date field for when the tool was last calibrated (ie; 2013-07-24). calib_freq is the interval frequency for calibration (ie; 180 days).

 

I'm currently querying the database like this:

  $callist = mysqli_query($con, "SELECT * FROM calibrated_items WHERE date_calib <= (NOW() - INTERVAL 180 DAY)");

I know this a jacked up way to try and accomplish what I'm aiming for. I just don't know enough to figure out how to use NOW() date_calib and and calib_freq to output a number of days until due field. I don't really need to save that field, I just want to output it to the webpage.

 

I hope I included enough information for this to make sense. If not, let me know. Thanks!

 

 

Link to comment
Share on other sites

  • Solution
SELECT *,
       DATEDIFF(DATE_ADD(date_calib, INTERVAL calib_freq day), NOW()) AS days_till_calib
FROM calibrated_items

This will give you ALL the records fom the database along with the number of days until calibration is needed. A negative number will mean it has past the date

 

EDIT: To explain this. We first take the last calibration date and the calibration frequency and use DATE_ADD() to calculate the next date that calibration is due. Then we take that date and NOW() and use the DATEDIFF() function to calculate the number of days between now and the next calibration date.

 

EDIT #2: I just realized there is a simpler solution. Calculate the number of days that have passed since the last calibration (using DATEDIFF() ), then just subtract that from the frequency.

 

SELECT *,
       (calib_freq - DATEDIFF(NOW(), date_calib)) AS days_till_calib
FROM calibrated_items
Edited by Psycho
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.