• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

MySQL WHERE & timestamp help

I have a column called "timestamp" in my mysql table, the column type is "datetime":
Example timestamp format: 2011-12-16 16:51:45

I'm trying to write a SELECT query to only select records with a timestamp within the last 20 minutes of the current time.

How would I do this using PHP?

1 Solution
SharathData EngineerCommented:
try like this.
select *
  from your_table
 where DATE_SUB(curdate(),INTERVAL 20 MINUTE) <= timestamp

Open in new window

Greg AlexanderLead DeveloperCommented:
The php version could be

// store current date and time
$now = date("Y-m-d H:i:s");
// build a date and time variable twenty minutes in the past
$past = date("Y-m-d H:i:s",strtotime($now. "- 20 minutes"));

$query = "SELECT * FROM table_name WHERE timestamp >= '".$past;

Open in new window

sky_revolutionAuthor Commented:
Works great, thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now