I need to get the records which are inserted in last 24 hours. I have read about it and got that I need to use DATETIME
type in my MySQL date column.
But my problem is I don't know how to generate date for MySQL DATETIME
type with PHP ?
How can I do this? Thanks in advance.
Simply
$mysql_date_now = date("Y-m-d H:i:s");
Because DATETIME
in mysql is YYYY-MM-DD HH:ii:ss
If you are looking just for a way to generate the datetime to insert into the datetime field just use the following code
$datetime = date('Y-m-d H:i:s') ;
or for the direct sql
INSERT INTO {TABLE} ({DATETIME}) VALUES (NOW())
This will create a date that looks like
2012-02-28 16:44:25
If you are looking for the query to get the last 24 hours use
SELECT * FROM tablename WHERE date_field > NOW() - interval 1 day
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With