php 以日期时间格式插入当前日期 mySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9541029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:04:11  来源:igfitidea点击:

Insert current date in datetime format mySQL

phpmysql

提问by CCates

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.

它似乎没有成功工作,并且时间仍然是 00:00:00 如果您能找到很好的解决方案,谢谢。

回答by Aaron W.

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

如果您想存储当前时间,只需使用 MYSQL 的函数。

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:sso try

如果你需要使用 PHP 来做它,格式化它Y-m-d H:i:s所以尝试

$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");

回答by Erik Giberti

Try this instead

试试这个

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

回答by Gaurang

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

NOW()用于在 MySQL 表中插入当前日期和时间。所有具有数据类型的字段都DATETIME, DATE, TIME & TIMESTAMP适用于此功能。

YYYY-MM-DD HH:mm:SS

YYYY-MM-DD HH:mm:SS

Demonstration:

示范:

Following code shows the usage of NOW()

以下代码显示了使用 NOW()

INSERT INTO auto_ins
(MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
VALUES
(“NOW()”, NOW(), NOW(), NOW(), NOW(), NOW());

回答by Diar Selimi

you can use CURRENT_TIMESTAMP, mysql function

你可以使用 CURRENT_TIMESTAMP, mysql 函数

回答by Abdullah

set the typeof column named datepostedas DATETIMEand run the following query:

设置type名为datepostedas的列DATETIME并运行以下查询:

INSERT INTO table (`dateposted`) VALUES (CURRENT_TIMESTAMP)

回答by PratapSingh hajari

     <?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">

回答by Jan Han?i?

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

datetime”期望日期格式如下:YYYY-MM-DD HH:MM:SS

so format your date like that when you are inserting.

所以在插入时格式化你的日期。

回答by Salem

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

如果您从 PHP 传递日期,您可以使用STR_TO_DATE()mysql 函数使用任何格式。让我们考虑您通过 html 表单插入日期

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

The datepostedshould be mysql datetype . 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 . () .

dateposted应该是mysql的date类型。或者 mysql
在某些情况下会添加 00:00:00您最好将日期和时间一起插入数据库,以便您可以使用小时和秒进行计算。() .

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

回答by Prakash GPz

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

现在,当您在表中插入一行时,您可以跳过此字段 ( time_updated),因为它将以当前时间作为默认值填充

回答by Prakash GPz

Just use with your timezone :

只需使用您的时区:

date_default_timezone_set('Asia/Kolkata');      
$date=date("Y/m/d h:i:sa");