php time from database - php

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:
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?

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.

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

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.

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.

It's the server time.
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.

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

Related

What datatype should I use to store time() in MySQL for my application?

I plan to capture the start and end of user initiated activities on my website using time() in php. I'm not sure if this is the best way to capture start/end times. Anyway, the data will be stored in MySQL, but again I'm not sure what datatype I should use.
Based on the answers I've read on stackoverflow, the datatype used depends on the purpose of the application.
Purpose of the application
At it's simplest, I want to record start, stop (and duration) of an
activity. Probably using time().
At it's most complicated I'd like to plot statistics based on when
the user did a certain activity, how much time they spent doing the
activity (in total), and when they were the most successful/least
successful etc, etc. (all based on the start/end times) Something to
keep in mind. The users will be from all over the world.
MORE INFO
If an activity is repeated a new record will be made for it. Records will not be updated.
At first, I had planned on storing unix timestamps in MySQL (as an integer datatype?), but from what I understand this is a bad idea, because I will lose a lot of MySQLs ability to process the information. If I store the information as DATETIME, but then move the server, all the times will change based on the local time of the server. Something I found confusing was that TIMESTAMP in MySQL is not the same as a unix timestamp- which is what I would be getting if I used time().
I'm aware that the unix timestamp can only hold dates up to 2038 for some systems, but that isn't a concern (at the moment).
Question: Should I use time() to capture start and end times for user initiated activities? Based on the purpose of the application, what datatype should I use to store the start and stop of user initiated activities?
THANKS
Thanks for the answers everyone. TBH I'm not convinced either way yet, so I'm still doing some research. I chose the TIMESTAMPS option because I really would like to store my information using UTC (GMT). It's a pity though that I will lose out on some of MySQLs inbuilt time functions. Anyway thanks again for your answers.
If you're going worldwide, MySQL's TIMESTAMP is almost universally a good choice over DATETIME, since it stores the time as UTC instead of local time so DST changes won't cause you problems if analyzing in multiple time zones.
Having a non DST changing time zone as a base can be a life saver, converting between multiple time zones with different DST changeover dates can really cause problems, consider for example having a timestamp during the hour that happens twice in a change from summer- to winter time.
Use DATETIME to store the time and use date('Y-m-d H:i:s') to get the current time to store it. When you fetch this value, you will get the time in this format.
To convert it to a timestamp, use $timestamp=strtotime($fetchedValue) To display this in another format use date('H:i',$timestamp). Read about formats from date manual of php
TIMESTAMP can only store values after Jan 1 1970, since it stores timezone data.
So if you are trying to store a date before Jan 1 1970, its better to use DATETIME.
Frankly, TIMESTAMP is useful only if you are actively syncing raw data between two machines with different timezone

PHP/MySQL - confusion about storing timestamps

I need to store a timestamp for form submissions in a PHP/MySQL form. I would like to be able to query this field with the most flexibility for displaying the data in PHP. What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?
I would store this data as TIMESTAMP data type provided by MySQL. This has lots of advantages over other storage options such as DATETIME or INT listed below
It is especially meant to store a particular time instant. No matter which time zone your server is in, and which time zone your mysql client is in, the value of constant CURRENT_TIMESTAMP stored in a TIMESTAMP field will always point to the same instant in the absolute time line.
Internally it uses 4 bytes of space for storage, same as that of an INT data type
It will give you pretty looking, human understandable values in regular queries.
Moreover, these pretty looking values are converted to the timezone of the client connecting to it. This might be good or bad for you. You can always change it to a desired one, including UTC, with set timezone='timezone' if the mysql time zone table is populated or similar to SET time_zone='+5:30'. The latter would work only if there are no daylight saving adjustments in the desired timezone.
Your queries can take the benefit of the fact that MySQL understands this field represents a date. So you could run query like: Get all the registrations happened on month of may for last 3 years.
Use inbuilt functions to change the display format to show unix timestamp or any other valid form.
Use DATETIME or DATE in your database: http://dev.mysql.com/doc/refman/5.6/en/datetime.html
What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?
http://ch2.php.net/mysqli
dunno what timestamp you're talking about but for the unix timestamp you'll need INT type field.

Best practices to insert time/time stamp in mysql

I've had some trouble choosing an efficient way to insert a time in mysql database.
I know mysql has its own timestamp system but it is not what I'm looking for.
I currently use date("Y-m-d H:i:s", time()); and it gets the job done.
But I would like to know what the standard is or what the best practices are to insert time that make it easier for retrieval later.
Thanks.
ps. I've searched the site for any dupe posts but I could not find any, if you think this is a dupe feel free to close.
you insert it the way mysql likes to store it, and on retrieval, you format it how you like (DATE_FORMAT()). You don't store it in some other format because that is how you want to display it, as that then breaks all the functionality of the date\time.
The best thing to do is store dates and times in the database in UTC and convert them to the appropriate timezone when displaying them. MySQL even has a special function for "now in UTC": UTC_TIMESTAMP().
When you use PHP's date function to format a Unix timestamp, you are outputting the representation of the date and time in the web server's timezone. That is rarely what you want, and it causes all sorts of problems whenever the web server's timezone changes, such as during Daylight Saving Time or if the web server is physically relocated to a different timezone.
You can store them as a MySQL timestamp and then use UNIX_TIMESTAMP() to convert to unix time for use in PHP, you can also use FROM_UNIXTIME to convert from unix time to a MySQL timestamp when inserting.
SELECT UNIX_TIMESTAMP(`field`) AS `unix_timestamp` FROM `table`;
INSERT INTO `table` SET `field` = FROM_UNIXTIME(unix_timestamp);

What's the best way to adjust for local timezones?

Maye I am wrong to use now() to get timestamps when storing data?
When I display date/time to the user, of course he wants to see a local time, and if he inputs time related data, rather than me using now() then he will be inputting local date/time.
Why code is getting muddled with conversations - what's the best practice for handling timestamps? UTC/locla time? How & when to adjust?
Just store all the dates in timestamp mysql column type, which can handle all timezone issues, and mysql will do all work for you.
So in the begin of your application startup all you need is to specify what timezone you need the dates belongs towith query:
SET time_zone='Asia/Vladivostok'
For example.
Also, in this case you should not get any timestamps from php, if you need to insert current time - you have to use mysql's NOW().
That's all.
Store all the dates in one time zone so they're consistent. UTC/GMT+0 is good for this.
Then use CONVERT_TZ to convert input to UTC/GMT+0 or from UTC/GMT+0 to a user's time zone.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Since all you've asked about are SQL functions, you should tag the question with the RDBMS you're using (MySQL?) instead of PHP.

Why not use MySQL's TIMESTAMP across the board?

If you need your web application to translate between timezones on a per-user basis, why not use TIMESTAMP across the board for all date/time fields? Consider that TIMESTAMP values are stored in UTC and converted to the timezone set for the connection when retrieved.
I have asked this question on IRC, read the MySQL documentation, searched Google extensively, and asked my co-workers, and I have yet to find a compelling reason to not use TIMESTAMP.
Note: I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case. Also, I am using PHP with MySQL.
DATETIME is for arbitrary dates and times that you utilize in your data.
TIMESTAMP is for when you want the time to be automatically updated. If you want to know when the row was inserted/updated, use a TIMESTAMP.
Also, keep in mind that a TIMESTAMP is only stored in UTC -- it is converted to the local timezone of the server before it is transmitted back as part of a query.
In your case, you're better off using a DATETIME. If you're worried about UTC, that's fine -- just create your dates in UTC rather than using the local time. Use UTC_TIMESTAMP.
I think your answer is here:
I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case.
I would be careful making assumptions about the longevity of projects especially when it comes to database schemas. Databases have a tendency to remain in place and in use long after the applications that used them have gone away.
Randalpho's answer is wrong on many facts!
Timestamps do not need to be automatically updated on creation OR updates.
Also, timestamps are translated to the CLIENT's local time, not the serever's.
Just look at the MySQL docs for datetime.

Categories