Ben Pearce
Ben Pearce

Reputation: 7094

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

Upvotes: 0

Views: 1853

Answers (2)

Roger Ng
Roger Ng

Reputation: 779

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.

Upvotes: 0

Ranjith
Ranjith

Reputation: 2819

You can use date() function in php, like this,

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

Upvotes: 1

Related Questions