Insert Current Date in Datetime Format MySQL

Insert current date in datetime format mySQL

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')");

How to insert current date in database mysql

You should not pass date or datetime from PHP if you want the date/datetime at the time of yours query's execution, instead you can use built-in MySQL functions to do so

To insert current date & time use NOW()

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, NOW()
FROM `shopping_cart` WHERE `userid` = $uid";

To insert current date only use CURRENT_DATE()

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
SELECT `product_id`,`pr_name`,`pr_price`,`pr_size`,
`userid`, CURRENT_DATE()
FROM `shopping_cart` WHERE `userid` = $uid";

These queries assume you have a column named dt in your delivery table. Please change column name if different in your schema, or add a date or datetime column in your table if you do not have any yet

How to get current date & time in MySQL?

You can use NOW():

INSERT INTO servers (server_name, online_status, exchange, disk_space, network_shares, c_time)
VALUES('m1', 'ONLINE', 'exchange', 'disk_space', 'network_shares', NOW())

mysql insert current date, only in insert time

you may use NOW() function in you respective fields

Inserting Current Date as default in MySQL table

st.executeUpdate( 
"insert into groupperformance (Date,Gid,GName)
select '" + yourDate + "', Gid,GroupName from grouping"
);

But, make sure that yourDate string is in valid MySQL date format YYYY-MM-DD.

And, if you want to insert current date from the database server, you can use now() or curdate() instead of your selected date.

st.executeUpdate( 
"insert into groupperformance (Date,Gid,GName)
select curdate(), Gid,GroupName from grouping"
);

INSERT current date or time into MySQL

Firstly, you should have a PRIMARY KEY in your table.

Secondly, you have not set default values for columns Date and Time. Also, you can't set them separately for the DATE and TIME types – you should use TIMESTAMP type and DEFAULT CURRENT_TIMESTAMP like :

 CREATE TABLE Register (
Name CHAR(20) PRIMARY KEY NOT NULL,
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Thirdly, if you want to use exactly two columns for date storing, you can set a trigger on INSERT event for this table, like it is shown below :

 CREATE TRIGGER default_date_time
BEFORE INSERT ON my_table_name
FOR EACH ROW
BEGIN
SET NEW.Date = CURDATE();
SET NEW.Time = CURTIME();
END;
$$

PHP date() format when inserting into datetime in MySQL

The problem is that you're using 'M' and 'D', which are a textual representations, MySQL is expecting a numeric representation of the format 2010-02-06 19:30:13

Try: date('Y-m-d H:i:s') which uses the numeric equivalents.

edit: switched G to H, though it may not have impact, you probably want to use 24-hour format with leading 0s.



Related Topics



Leave a reply



Submit