简体   繁体   中英

Inserting a “timestamp” formated entry to an mySQL table using php

I have a mySQL table with a column formatted as "timestamp". I am trying to insert a value using a php script to no avail. Below I have shown a couple unsuccessful approaches, can any one demonstrate how to do generate a properly formatted value using php?

$insert = mysql_query("INSERT INTO `tableName`(`timeColumnName`) VALUES ('".strtotime("now")."')") or die(mysql_error());

$insert = mysql_query("INSERT INTO `tableName`(`timeColumnName`) VALUES (now())") or die(mysql_error());

您可以像这样在php使用date()函数,

$insert = mysql_query("INSERT INTO tableName(timeColumnName) VALUES ('".date("Ymd H:i:s")."')") or die(mysql_error());

This is a simple example for you to insert the timestamp. It depends which time format you would like to store in the MySQL DB.

$mysqldate = date( 'Y-m-d H:i:s', now() );
$query = "UPDATE table SET datetimefield = $mysqldate WHERE...";

If you wish to store the timestamp as a number, you have to change the type of your field in DB to number type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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