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
Find First Character That Is Different Between Two Strings
PHP 5.4 - 'Closure $This Support'
Get the First Letter of Each Word in a String
How to Decrypt a Password Hash in PHP
Notice: Undefined Offset: 0 In
Update Query with Pdo and MySQL
Output Is in Seconds. Convert to Hh:Mm:Ss Format in PHP
Which Is the Best Way to Generate Excel Output in PHP
How to Format Excel File with Styles, Fonts, Colors, Tables etc with Pure PHP
How to Write a Stored Procedure in PHPmyadmin
How to Remove "Via" and Server Name When Sending Mails with PHP
PHP Return 500 Error But No Error Log
Improve Password Hashing with a Random Salt
Can PHP's Glob() Be Made to Find Files in a Case Insensitive Manner