What time the server is on, and why PHP and MySQL can disagree

A form that records the wrong time, an order dated yesterday, a scheduled job firing two hours early — it is nearly always the same thing: a server does not have one clock, it has three, and they do not have to be in the same time zone.

The three clocks

Clock Who uses it
The system one The machine's time. It appears in server logs and in file timestamps.
PHP's What your site uses when it calls date(). This is the one that rules your site, and it need not match the system.
The database's What MySQL uses in NOW() and CURRENT_TIMESTAMP.
This is where mismatched times are born. If one part of your site records the time with PHP and another lets the database fill it with NOW(), and the two clocks are not in the same zone, you end up with different times recorded for the same event. Nothing errors — it only surfaces when somebody notices the numbers do not add up. If your site records dates, it is worth confirming this before you need them.

How to find yours, in two minutes

Do not guess: the value that matters is your account's. Create a file called time.php in public_html with these lines:

<?php
echo 'PHP: ' . date('Y-m-d H:i:s T') . ' (' . date_default_timezone_get() . ')<br>';
$db = new mysqli('localhost','USER','PASSWORD','DATABASE');
echo 'MySQL: ' . $db->query('SELECT NOW()')->fetch_row()[0];
1 Replace the user, password and database name with your account's — see creating databases and using phpMyAdmin.
2 Open yourdomain.co.mz/time.php in the browser.
3 Compare the two lines. If they do not match, that is the problem you have been chasing.
4 Delete the file afterwards. It holds your database password — it cannot stay there.

How to put your site on the time you want

You do not have to ask us for anything: each account can choose its own, and it affects nobody else.

Where How
WordPress Under Settings → General, pick the city from the time zone list. WordPress then shows everything in that time, whatever the server does. It is the simplest route and covers most cases.
Your own PHP site One line at the start: date_default_timezone_set('Africa/Maputo') — or whichever city suits you.
The whole domain at once In cPanel, under MultiPHP INI Editor, set date.timezone for the domain. It then applies to every PHP file on it — see how to change your PHP version
Just one SQL query Convert inside the query itself, with CONVERT_TZ().
The right zone for these markets. Use Africa/Maputo for Mozambique and Africa/Luanda for Angola. Do not write «UTC+2» or «UTC+1» by hand: the city name is better because it follows any future rule change by itself. Neither country observes daylight saving, but anyone with visitors in Europe gains from using the name, because there the clock changes twice a year.

Times in e-mail and in logs

Two things you cannot change from the site, and it helps to know:

1 In e-mail messages, the time travels with its zone in the header, and each mail program shows it converted to the reader's zone. That is why the same message appears at different times on different computers — and that is correct.
2 In server logs — the ones we open when you ask for help — the time is the system's. When you write to us about a problem, give the time and the zone you saw it in. It saves us searching two hours away from it.

If you need to rule the whole clock

On a shared plan the system time is common to the whole machine and is not changed per account — what you change is PHP's and your queries', which is what matters to 99% of sites. If your application genuinely requires the entire system in another zone, that means a server of your own: see shared hosting vs VPS.

Times on your site not adding up? Show us the example.

Open a ticket

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $10.00/mo

See plans
  • 0 Users Found This Useful
Was this answer helpful?