在datetime格式mySQL中插入當前日期。

[英]Insert current date in datetime format mySQL


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,謝謝。

13 个解决方案

#1


161  

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:s so try

如果您需要使用PHP來完成它,那么它的格式是:i:s so try。

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

#2


27  

Try this instead

試試這個相反

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

#3


5  

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.

現在()用於在MySQL表中插入當前日期和時間。所有具有數據類型DATETIME、日期、時間和時間戳的字段都能很好地處理這個函數。

YYYY-MM-DD HH:mm:SS

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());

#4


2  

you can use CURRENT_TIMESTAMP, mysql function

可以使用CURRENT_TIMESTAMP, mysql函數。

#5


1  

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

所以當你插入的時候,格式化你的日期。

#6


1  

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

將名為datepost的列的類型設置為DATETIME,並運行以下查詢:

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

#7


0  

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

#8


0  

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.

希望,將完美的工作。

#9


0  

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 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 . () .

datepost應該是mysql日期類型。或者mysql會在某些情況下增加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')"  ;  

#10


0  

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),因為它將填充當前時間作為默認值。

#11


0  

Just use with your timezone :

使用你的時區:

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

#12


-1  

use this

使用這個

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

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

and then in MYSQL do

然后在MYSQL中。

type=varchar

= varchar類型

then date and time will be successfully inserted

然后將成功插入日期和時間。

#13


-4  

Try this

試試這個

$('#datepicker2').datepicker('setDate', new Date('<?=$value->fecha_final?>')); 

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/03/02/72114c24e47aa52fb9bbebc409338171.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com