114

I'm having problems getting the date inserted properly into my database.

$date = date('m/d/Y h:i:s', time());

I use this format, and, it echoes out correctly, however, when, I insert

mysql_query("INSERT INTO table 
(dateposted) 
VALUES ('$date')");

it doesn't appear to work successfully, and, the time remains 00:00:00 If you could find the solution that would be great, thanks.

asked Mar 2, 2012 at 22:06
1
  • 2
    Dateposted is the datetime format. Commented Mar 2, 2012 at 22:09

15 Answers 15

257

If you're looking to store the current time just use MYSQL's functions.

mysql_query("INSERT INTO `table` (`dateposted`) VALUES (now())");

If you need to use PHP to do it, the format it Y-m-d H:i:s so try

$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");
answered Mar 2, 2012 at 22:11
Sign up to request clarification or add additional context in comments.

4 Comments

But it'll use MySQL server locale/timezone. If U have Apache/PHP and MySQL on different servers and timezone set incorrect in one of them U'll have different values.
@Jeff_Alieffson is right. Using UTC_TIMESTAMP() (dev.mysql.com/doc/refman/5.5/en/…) is preferred instead of NOW()
I tried same but I am getting 0000-00-00 00:00:00 and my datatype is datetime. any help in this?
Would be great to use PDO instead of mysql_query(), wouldn't?
39

Try this instead

$date = date('Y-m-d H:i:s');
answered Mar 2, 2012 at 22:11

Comments

9

NOW() is used to insert the current date and time in the MySQL table. All fields with datatypes DATETIME, DATE, TIME & TIMESTAMP work good with this function.

YYYY-MM-DD HH:mm:SS

Demonstration:

Following code shows the usage of NOW()

INSERT INTO auto_ins
(MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
VALUES
("NOW()", NOW(), NOW(), NOW(), NOW(), NOW());
reigeki
3911 gold badge5 silver badges19 bronze badges
answered Oct 19, 2012 at 6:02

Comments

7

you can use CURRENT_TIMESTAMP, mysql function

answered Jun 20, 2013 at 21:25

Comments

6

set the type of column named dateposted as DATETIME and run the following query:

INSERT INTO table (`dateposted`) VALUES (CURRENT_TIMESTAMP)
Sufiyan Ghori
18.9k17 gold badges85 silver badges111 bronze badges
answered Mar 4, 2014 at 13:53

Comments

2

"datetime" expects the date to be formated like this: YYYY-MM-DD HH:MM:SS

so format your date like that when you are inserting.

answered Mar 2, 2012 at 22:10

2 Comments

So I would do date("YYYY/MM/DD HH:MM:SS", time());?
No? use dashes(-) not slashes (/).
1
 <?php $date= date("Y-m-d");
$time=date("H:m");
$datetime=$date."T".$time;
mysql_query(INSERT INTO table (`dateposted`) VALUES ($datetime));
?>
<form action="form.php" method="get">
<input type="datetime-local" name="date" value="<?php echo $datetime; ?>">
<input type="submit" name="submit" value="submit">
answered Sep 24, 2015 at 18:36

1 Comment

Welcome to Stack Overflow! Good answers explain as well as provide code. Consider updating your answer to include an explanation about how this code works and why it is the best option.
1

If you Pass date from PHP you can use any format using STR_TO_DATE() mysql function . Let conseder you are inserting date via html form

$Tdate = "'".$_POST["Tdate"]."'" ; // 10/04/2016
$Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y')" ; 
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$Tdate')");

The dateposted should be mysql date type . or mysql will adds 00:00:00
in some case You better insert date and time together into DB so you can do calculation with hours and seconds . () .

$Tdate=date('Y/m/d H:i:s') ; // this to get current date as text .
$Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y %H:%i:%s')" ; 
answered Apr 26, 2016 at 11:15

Comments

1

The best way to make it easier and efficient is this:

CREATE TABLE table_name (
field1,
..
..
time_updated timestamp default current_timestamp
)

Now, when you insert a row into table, you can skip this field (time_updated) as it will fill with current time as default value

answered Nov 26, 2016 at 10:37

Comments

1

It depends on what datatype you set for your db table.

DATETIME (datatype)

MYSQL
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// This will insert date and time into the col. Do not use quote around the val
PHP
$dt = date('Y-m-d h:i:s');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert date into the col using php var. Wrap with quote.

DATE (datatype)

MYSQL
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// Yes, you use the same NOW() without the quotes.
// Because your datatype is set to DATE it will insert only the date
PHP
$dt = date('Y-m-d');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert date into the col using php var.

TIME (datatype)

MYSQL
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// Yes, you use the same NOW() as well. 
// Because your datatype is set to TIME it will insert only the time
PHP
$dt = date('h:i:s');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert time.
answered Sep 25, 2020 at 14:27

Comments

0

Just use with your timezone :

date_default_timezone_set('Asia/Kolkata'); 
$date=date("Y/m/d h:i:sa");
answered Jun 19, 2017 at 17:11

Comments

0

$date = date('Y-m-d H:i:s'); with the type: 'datetime' worked very good for me as i wanted to print whole date and timestamp..

$date = date('Y-m-d H:i:s'); $stmtc->bindParam(2,$date);

answered Nov 11, 2018 at 1:27

Comments

-1

I believe, you need to change your code little bit like follows alongside with your database filed type.

mysql_query("INSERT INTO table (`dateposted`) VALUES ($datetime)");

Hope, will work perfectly.

answered Sep 25, 2015 at 6:44

Comments

-3

use this

$date = date('m/d/Y h:i:s', time());

and then in MYSQL do

type=varchar

then date and time will be successfully inserted

Matthew Ciaramitaro
1,1791 gold badge13 silver badges27 bronze badges
answered Oct 3, 2017 at 5:25

2 Comments

your first function is the same as used in the question. And the rest of this answer doesn't warrant reopening a 5 year old question. Please explain why you think your solution is better than those that were posted 5 years ago.
because in old question not explain about data type
-5

Try this

$('#datepicker2').datepicker('setDate', new Date('<?=$value->fecha_final?>')); 
Saranjith
11.7k6 gold badges77 silver badges133 bronze badges
answered Sep 8, 2016 at 19:00

1 Comment

This will set a jQuery datepicker. The question was about inserting into a MySQL database.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.