PHP与MySQL时间不一致求助:如何调整GoDaddy主机上的MySQL时间
Hey there, I’ve run into this exact timezone mismatch issue on GoDaddy before—total headache when your PHP and MySQL timestamps don’t line up! Since you’ve already got PHP’s timezone sorted, let’s get MySQL aligned with it. Here are the most reliable methods depending on your hosting plan:
1. Session-Level Timezone Set (Best for Shared Hosting)
GoDaddy’s shared hosting often locks down global MySQL settings, so this is the safest bet. Every time your PHP script connects to MySQL, run a quick query to set the timezone to match PHP’s:
First, confirm your PHP timezone to be precise—run this in a test PHP file:
echo date_default_timezone_get();
It’ll output something like 'America/Chicago' or 'Asia/Shanghai'. Then, right after establishing your database connection in your code, add:
// Assuming $conn is your MySQL connection object $conn->query("SET time_zone = 'YOUR_PHP_TIMEZONE';");
This ensures every database operation in that session uses the correct timezone, matching your PHP timestamps.
2. Global Timezone Set via phpMyAdmin (If Permitted)
If you have access to modify global settings (more likely on VPS/dedicated plans):
- Log into your GoDaddy cPanel and open phpMyAdmin
- Switch to the SQL tab
- Run this query, replacing the timezone with your PHP’s:
SET GLOBAL time_zone = 'YOUR_PHP_TIMEZONE';
Note: This might require elevated privileges—if you get an error, stick to the session-level method.
3. Edit MySQL Configuration File (VPS/Dedicated Only)
For self-managed plans, you can set the default timezone permanently in MySQL’s config:
- Locate
my.cnf(ormy.inion Windows) in your server’s MySQL directory - Add this under the
[mysqld]section:
default-time-zone = 'YOUR_PHP_TIMEZONE'
- Restart the MySQL service for changes to take effect. Keep in mind GoDaddy might restrict restart access on some plans, so check their control panel options first.
One quick check to confirm it works: After making changes, run SELECT NOW(); in MySQL and compare it to date('Y-m-d H:i:s') in PHP—they should now match perfectly!
内容的提问来源于stack exchange,提问作者Marcos Felipe

