[PHP-DB] Php datetime
hi, i have created a mysql table where i would like to store the date and time whenever a user logs into a web site. the login is done through php/mysql as well but i'm not sure what the php code is to add the current date/time into a mysql database. the field in the db is created as a 'datetime' field. Regards, Gawie Marais ..: GamCo :.. -- Tel: +27 11 312 0481 Fax: +27 11 312 0483 / +27 82 239 2986 http://www.gam.co.za -- if(empty($beer)) { getbeer() } else { consume($beer) } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Php datetime
You can do it in SQL: INSERT ... SET col = NOW() Ignatius _ - Original Message - From: ..: GamCo :.. Gawie Marais [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, March 04, 2004 14:35 Subject: [PHP-DB] Php datetime hi, i have created a mysql table where i would like to store the date and time whenever a user logs into a web site. the login is done through php/mysql as well but i'm not sure what the php code is to add the current date/time into a mysql database. the field in the db is created as a 'datetime' field. Regards, Gawie Marais ..: GamCo :.. -- Tel: +27 11 312 0481 Fax: +27 11 312 0483 / +27 82 239 2986 http://www.gam.co.za -- if(empty($beer)) { getbeer() } else { consume($beer) } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Php datetime
You also have the option of changing the column type to TIMESTAMP. With a TIMESTAMP column you don't have to do anything to get it to update automatically. Just do an INSERT into the table and the column will be updated automatically. Here's a snippet, but check the full MySQL documentation for more information.: Automatic updating of the TIMESTAMP column occurs under any of the following conditions: The column is not specied explicitly in an INSERT or LOAD DATA INFILE statement. The column is not specied explicitly in an UPDATE statement and some other column changes value. (Note that an UPDATE that sets a column to the value it already has will not cause the TIMESTAMP column to be updated, because if you set a column to its current value, MySQL ignores the update for efficiency.) You explicitly set the TIMESTAMP column to NULL. -Original Message- From: ..: GamCo :.. Gawie Marais [mailto:[EMAIL PROTECTED] Sent: Thursday, March 04, 2004 8:35 AM To: [EMAIL PROTECTED] Subject: [PHP-DB] Php datetime hi, i have created a mysql table where i would like to store the date and time whenever a user logs into a web site. the login is done through php/mysql as well but i'm not sure what the php code is to add the current date/time into a mysql database. the field in the db is created as a 'datetime' field. Regards, Gawie Marais ..: GamCo :.. -- Tel: +27 11 312 0481 Fax: +27 11 312 0483 / +27 82 239 2986 http://www.gam.co.za -- if(empty($beer)) { getbeer() } else { consume($beer) } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Php datetime
I've heard you want to use a TIMESTAMP field for that. i have created a mysql table where i would like to store the date and time whenever a user logs into a web site. the login is done through php/mysql as well but i'm not sure what the php code is to add the current date/time into a mysql database. the field in the db is created as a 'datetime' field. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Php datetime
Daniel Clark wrote: i have created a mysql table where i would like to store the date and time whenever a user logs into a web site. the login is done through php/mysql as well but i'm not sure what the php code is to add the current date/time into a mysql database. the field in the db is created as a 'datetime' field. I've heard you want to use a TIMESTAMP field for that. The MySQL TIMESTAMP is actually a pretty lousy column type. The only interesting thing that the TIMESTAMP column can do is auto-update, but this is pretty easy to do either (1) using MySQL's NOW() function or (2) formatting a php (unix) timestamp. (1) Use MySQL's NOW() function: INSERT INTO mytable (col1, datetime_col) VALUES ('blah', NOW()) (2) Format the date using PHP's date() function: $sql = INSERT INTO mytable (col1, datetime_col) VALUES ('blah', '.date('Y-m-d H:i:s', time()).') I would recommend the MySQL method unless you have a real reason to use PHP for the stamp. Oh, I almost forgot. Why is TIMSTAMP bad? Because (1) it has nothing to do with the meaning of 'timestamp' in any other DB and (2) it's a real pain to parse the value of a MySQL TIMESTAMP for use in your PHP application. TIMESTAMP is in the format YYYMMDDHHMMSS (e.g. 20040304100655). strtotime() will not deal with that value. It looks like a number. The only way (I've found) to turn that into something readable is to use MySQL time/date functions to format it or to use regular expressions (or other string parsing in PHP). Using the date functions really binds you to using MySQL and also makes it impossible to format the date in another language/locale. IMO, formatting dates in SQL is just plain bad practice. Hans -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php