简体   繁体   中英

Any disadvantage using SET time_zone in MySQL PHP

I am using timestamp fields in my databases and my PHP software has its own time management system based on users timezone. I want to use timestamp fields for certain kind of data (created or modified when) and also be able to use te DEFAULT CURRENT_TIMESTAMP for the columns.

Is there any disadvantage setting the timezone to UTC using, SET time_zone = '+00:00' , each time the session is created. I have four separate databases which the software uses and currently, I am setting the current timezone to UTC.

I don't want to use DATETIME as they are larger in size and also I won't be able to use DEFAULT CURRENT_TIMESTAMP as the timezone of the server might have an offset.

You should not use SET time_zone if your backend already uses all the logic into converting user's timezone correctly, because you're wasting resources unnecessarily. The UTC timezone should be into the metadata of the DB, where always the DB transactions will work with them.

By the way, TIMESTAMP columns always will be stored in UTC , so you don't need to setting that, unless your columns are datetime (not the case, i think).

When you insert a TIMESTAMP value, MySQL converts it from your connection's time zone to UTC for storage. When you query a TIMESTAMP value, MySQL converts the UTC value back to your connection's time zone. Notice that this conversion does not occur for other temporal data types such as DATETIME.

So you have two options:

  • Set the timezone in your transactions working with time in your sql;
  • Working with unix timezones into backend and only showing the correct converted time in the frontend to user.

I prefer the second one.

When dealing with date/time for entry date and/or modified date, it is better to use normal VARCHAR with the length of around 200 (or any other value that fits the full date) in order to store the full date and process your date/time in your PHP script. This gives you the flexibility to view your time based on the timezone defined in your PHP code. Click here to see available timezones in PHP.

You can also format the date/time in any possible format you want by simply using the date_format of PHP.

I have given a reference code below.

//This is the way you define your timezone in PHP code
date_default_timezone_set('Asia/Beirut');

//You can capture the date/time by using the below code. This will store "2017-05-28 23:55:34"
$date_time_registered = date('Y-m-d H:i:s');

//Retrieve the date/time and re-format it as you require. Below code will output "May", full month.
$retrieve_month_only = date_create($row['your_store_date_time']);
$retrieve_month_formatted = date_format($retrieve_month_only, 'F');

echo $retrieve_month_formatted;

You can refer to this link to find out about PHP date/time formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM