Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate date for MySQL DATETIME type with PHP?

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.

like image 536
JohnUS Avatar asked Feb 28 '12 21:02

JohnUS


2 Answers

Simply

$mysql_date_now = date("Y-m-d H:i:s");

Because DATETIME in mysql is YYYY-MM-DD HH:ii:ss

like image 179
apelliciari Avatar answered Oct 11 '22 22:10

apelliciari


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
like image 22
bretterer Avatar answered Oct 11 '22 23:10

bretterer