user780483
user780483

Reputation: 3063

php time from database

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:

  1. Does the time come from the user's computer or somewhere on my server or somewhere else?
  2. Is the time function stored as an Int the best way to do this?

Upvotes: 2

Views: 291

Answers (6)

Emil Vikström
Emil Vikström

Reputation: 91902

  1. It's the server time.
  2. It depends on what you are going to do with it. An SQL query for a certain month may be better served by a DATETIME field.

Upvotes: 1

Diego Agulló
Diego Agulló

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

user336883
user336883

Reputation:

It's best record int in DB and use mktime function to parsing in the page.

Upvotes: 0

Sabeen Malik
Sabeen Malik

Reputation: 10880

Does the time come from the user's computer or somewhere on my server or somewhere else?

  • Its based on the server time

Is the time function stored as an Int the best way to do this?

  • As the value returned is int, storing in int is the way to go.

Upvotes: 3

Kissaki
Kissaki

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

steven
steven

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

Related Questions