SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Collecting records of current date or from last x seconds

We will try to get all records inserted or updated today. We will use the date and time field which stores the date and time data of the records. While comparing we will use MySQL curdate() function which returns today's date. Here is the query.

SELECT * FROM `test_time2` WHERE tm2 = curdate()

Here our tm field stores date data only. If we are storing date and time both then we have to change the query by adding a greater than equal to comparison.

SELECT * FROM `test_time` WHERE tm >= curdate()

This way we can collect records of present date. We can use DATE_SUB() functions to get the records of last 7 days or 15 days or X days.

Getting all records of last 10 minutes or 5 seconds or last one hour.

We can collect records added or updated in last X seconds by using this query.

SELECT * FROM test_time2 WHERE ( unix_timestamp( ) - unix_timestamp( tm ) ) < 5

The above query will return all records updated within last five seconds. This can be changed to X seconds.

You can modify the above query to return records of last one hour or any time multiples in seconds. This is required in preventing spam postings which automatically adds new posts or replies. We can check this by stopping posted within a particular duration.


Further readings
Date & Time functions used in Query for MySQL Table
Getting formatted date value from date field in MySQL
date_add function to calculate date & time of MySQL table
Formatting date and time before adding to date field of MySQL
String data to Date & time Format by using str_to_date
Formatting string data stored in varchar field to date value
Automatically updating / inserting current date and time value in a DATETIME field
Records of each day by using group by command
Collecting records between two date ranges from MySQL table fields
Getting date values from MYSQL table in readable format including time
Difference in days between two date fields
Getting the year part from date field
Getting the month part from date field
Getting the day part from date field
Records of today or records of last X seconds by using curdate() or unix_timestamp
Records of last one month from today by using date field
Records of present week days by using dayofweek function
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
Date & Time
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2013 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer