开发者

Datetime field - MySQL and PHP

How can I insert the current date into a datetime field in MySQL database?

I have the code to connect to the database, make a query, and execute the query in php (which I've done plenty of times), but I've never dealt with dates in SQL or in PHP. Here's what I tried, but it's just returning 0 for the time:

<?php
$con = mysql_connect("somewebsite.com","user","somepassword");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("someuser", $con);

$mysqldate = date( 'Y-m-d H:i:s', $开发者_如何学编程phpdate );
$phpdate = strtotime( $mysqldate );

$query = "insert into sometable
            (field1, field2, field3, date) 
          values 
            ('" . $_GET['name'] . "', '" . $_GET['pswd'] . "', '0', " . $phpdate . ")";
echo $query;
$result = mysql_query($query);

mysql_close($con);
?>


$query = "insert into sometable (field1, field2, field3, date) 
           values ('" . $_GET['name'] . "', '" . $_GET['pswd'] . "', 
           '0', CURRENT_TIMESTAMP)";

CURRENT_TIMESTAMP is the key, MySQL does it all for you.


$date = date( "Y-m-d H:i:s", time() );

or, for $phptime

$date = date( "Y-m-d H:i:s", strtotime( $phptime ) );

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜