I have a database that I would like to keep track of who and when people are logging on. I created a table called users. It has three fields, id, user, date. I created this script that is executed when they log in.

$db = mysql_connect("","","");


$today= getdate();

$query = "INSERT INTO users VALUES ('','$user','$today')";


The script enters the user ok, but enters a zero value for the date and time.

If I add print_r($today); to the script, it returns the correct date and time to the browser, but not the database.

Thanks in advance for any help!


$today becomes an array, so it itself has no value

What I suggest is to turn $today from an array to a string by doing this:

$today = serialize($today); // turns into a string

Then insert $today into the database like you normally would. In order to get the data back from $today, just unserialize before using, like so:

$user = mysql_fetch_array(mysql_query("SELECT * FROM users");

$today = unserialize($user['today']);

// $today is now useable again!

I updated my script as follows.

db = mysql_connect("","","");


$today= getdate();

$today = serialize($today);

$query = "INSERT INTO users VALUES ('','$user','$today')";

It still gives me a zero value in the table.
I have the field type set to timestamp. Is this correct?

Thanks for your help!


Field type should be something like TEXT.

If you want timestamp, then you need a UNIX timestamp. So do something like

$query = "INSERT INTO users VALUES ('','$user',time())";