简体   繁体   English

使用PHP在MySQL中保存日期时间

[英]save datetime in mysql using php

I have table in mysql database which have field with datatype is datetime. 我在mysql数据库中有表,其数据类型为datetime。

I want to save the datetime in this minute, for this I use " Now() ", but it does not work, 我想在这一分钟内保存日期时间,为此,我使用“ Now()”,但是它不起作用,

It just save 000000000000 in databaes. 它只是在数据库中保存了000000000000。

If you use php , the correct format is: 如果使用php ,则正确格式为:

date("Y-m-d H:i:s");

UPDATE: Minutes are expressed as i not m UPDATE:分表示为i并不m

If you've got a timestamp in PHP check out FROM_UNIXTIME() http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime 如果您有PHP时间戳,请查看FROM_UNIXTIME() http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime

$tstamp = time();

$query = "INSERT INTO `table` VALUES (FROM_UNIXTIME($tstamp))";
INSERT ... (Now(), ...)

在函数Now()周围没有附加引号

我也会使用time()函数,然后使用date()输出不同类型的时间戳很容易。

$query = "INSERT INTO tbl VALUES (".time().");";

date("g") this will return 12-hour format of an hour without leading zeros. date("g")这将返回一个小时的12小时格式(不带前导零)。 For more options see http://php.net/manual/en/function.date.php 有关更多选项,请参见http://php.net/manual/zh/function.date.php

try this: set the 'type' of column named 'date_time' as 'DATETIME' and run the following query: 尝试以下操作:将名为“ date_time”的列的“类型”设置为“ DATETIME”,然后运行以下查询:

INSERT INTO my_table ( date_time ) VALUES (CURRENT_TIMESTAMP) 插入my_table( date_time )VALUES(CURRENT_TIMESTAMP)

An example of a PHP script which sets a date in MySQL manually, 一个PHP脚本示例,它在MySQL中手动设置日期,

<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>

An example of a PHP script which sets a date in MySQL Automatic, 一个PHP脚本示例,该示例在MySQL Automatic中设置日期,

<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()',  CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM