Reputation: 3063
I use the php time()
function to insert the time into a database after a user posts on my site. I store this value as an int. I believe the time()
function inputs the number of seconds since like 1970. I now want to get the current time so I can subtract the two and get the time since the user's post. I have a two questions regarding this process:
Upvotes: 2
Views: 291
Reputation: 91902
Upvotes: 1
Reputation: 9576
You may consider using a DATETIME
column type to store this kind of data. It's pretty easy to recover the values in the way you want (ie: as an UNIX timestamp using MySQL's UNIX_TIMESTAMP
function, formatted date/time using DATE_FORMAT
...).
You could in some way override UNIX timpestamp's 1970 limit, for example storing and showing some guy's birthdate prior to 1970. Of couse, if you just store the data in a DATETIME
column and use UNIX_TIMESTAMP
to obtain an int, you did nothing.
Upvotes: 1
Reputation:
It's best record int in DB and use mktime function to parsing in the page.
Upvotes: 0
Reputation: 10880
Does the time come from the user's computer or somewhere on my server or somewhere else?
Is the time function stored as an Int the best way to do this?
Upvotes: 3
Reputation: 9217
See the PHP doc on time.
The time
function returns an int, so using that and saving it is the simplest and most efficient way.
As PHP is executed on the server the servers time is used. Make sure to set your timezone in your PHP configuration. If you did not, using a verbose log level should warn you about it.
Upvotes: 1
Reputation: 4875
the php time function refers to the severs internal time. you can use timestamp or datetime for storing the time in DB. From my experience both have a well performance.
Read about differences of time types: http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Upvotes: 1