Jump to content

Calculating an older date/time based on a desired interval


FreakingOUT

Recommended Posts

I've been researching online resources and seen a lot of code and functions for calculating an interval or difference between two date/time figures.  What I'm trying to do is somewhat in reverse... I want to establish an interval to determine a past date/time to be used in a MySQL Query but just can't figure the critical part.

// STEP #1 = DEFINE THE DESIRED DATE/TIME DIFFERENCE INTERVAL DESIRED FOR QUERIES

$timeinterval = '60'; // Interval is in MINUTES and can be changed as desired

// STEP #2 = GET THE CURRENT LOCAL SERVER DATE/TIME
// as YYYY-MM-DD HH:MN:SE

$serverdatetime = date('Y-m-d H:i:s'); // My local Server Date & time

// STEP #3 = CALCULATE THE ***PAST** (OLDER) DATE/TIME LIMIT BASED ON $timeinterval (Minutes)
// as YYY-MM-DD HH:MN:SE

$pastdatetime = [stuckinarut here] <- 60 MINUTES prior to current Server Date/Time

// STEP #4 = MySQL QUERY (`submitted` column is auto-timestampped on record insertions)

$sql="[columnsblahblah] FROM mydb WHERE `submitted` >= $pastdatetime";

In a Perfect World, the MySQL Query would yield ONLY records with a `submitted` DATE/TIME equal to 60 Minutes (or less) PRIOR TO from the Current Server Time.

 

Any assistance is appreciated!

 

Thanks.

 

-FreakingOUT

 

 

 

Use MySQL's functions

...WHERE submitted > NOW() - INTERVAL 60 MINUTE

edit: To calculate using PHP

$pastdatetime = date('Y-m-d H:i:s', strtotime('-60 minutes'));

 

Hello, Barand:

 

Once again you have kindly come to my rescue!

 

As I was pouring through multiple lines of code in various function examples trying to figure out some type of 'reverse engineering', everything became a blurrrr on only 2 hours of sleep.  Now I am blown away (again) by the simplistic elegance of your concise solutions.  Another learning experience for me!

 

I tested both and really appreciate the two options you provided.  Using $pastdatetime in the PHP code will keep what is already a very lengthy $sql querey shorter.  And in conjunction with this in the page header...:

 

<meta http-equiv="refresh" content="3600">

 

... the desired content *should* always display automatically as desired and can easily be changed.

 

Thank you very, very much !!!

 

-FreakingOUT <- (No more in this time related matter :^)

OOOOOPS...

And in conjunction with this in the page header...:

<meta http-equiv="refresh" content="3600">

... the desired content *should* always display automatically as desired and can easily be changed.

No, that would limit the page refresh to one time per hour... DUH! 

 

Excuse the bandwidth.

 

-FreakingOUT

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.