Re: Php datetime

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux