Jump to content

Recommended Posts

I have a database table 'selections' which consists of various fields including 'startTime' in datetime format (YYYY-MM-DD HH:MM:SS).

 

I am writing a script that will be running automatically every so often and I need it to pull a record from the above table where the record is the next record with a 'startTime' within the next 10 minutes.

 

I am not sure on the best way to construct the query, using either php or mysql, without having to mess around with the date format too much.  I am not that familiar with the mysql functions but I think that would be the easiest way.  I have this much:

 

SELECT * FROM selections WHERE startTime ...(in the next 10 minutes)...ORDER BY startTime ASC LIMIT 1

 

Just not sure on the middle bit  ???

Link to comment
https://forums.phpfreaks.com/topic/52691-solved-php-or-mysql-for-this-query/
Share on other sites

MySQL can't internally handle text dates (well, in this case, I'm assuming), but php can (strtotime).  The problem is, it would be a huge performance hit if you pulled all the data and it was a bunch of rows....

 

I would suggest using something like a unixtimestamp or one of mysql's built in date fields for this....

 

That way comparing them would be easy, and when ever you wanted to show them in a common format you could just use date(format, timestamp).

You can always create the time in the format you need.

 

www.php.net/date

 

<?php
$time = date('Y-m-d h:i:s', time()+60*10); // next 10 minutes

?>

 

Than insert that into the query for the check, do not quote me on the format though look at how to correctly code for the one you need at the www.php.net/date site.

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.