After setting up a new Linux server, making a major change on the server, or when clocks go forward or back for daylight saving time, you sometimes need to reconfigure the Linux server's date and time.
This inevitably becomes a problem, especially on a web server hosted abroad.
There are a few commands I use to get around this that generally do the job, so I decided to turn them into an article, both for my own future reference and as a resource for anyone facing a similar problem.
You must run all the commands below as the superuser (root). Let's become root:
sudo su
First, let's check the server's local date and time:
date
It will print output similar to the following:
Sat Apr 1 22:55:10 EDT 2016
We will set the date and time we want using the same format:
date -s "01 APR 2016 23:56:00"
The command above sets the operating system date to April 1, 2016 and the time to 23:56:00 (23: hours, 56: minutes, 00: seconds).
Although configuring the system clock is generally enough, you also need to configure the hardware clock so that every application gets the correct date and time. To do so, we will run the following command to set the hardware clock to UTC based on the operating system's current time:
hwclock --systohc
If you haven't made any mistakes so far, you're done; all that's left is to test our configuration. We expect the output of the following commands to be the same:
hwclock --show --utc
Sat 01 Apr 2016 23:56:55 EDT -0.239872 seconds
and
date
Sat 01 Apr 2016 23:56:55 EDT
In addition, don't forget to configure the date settings inside your PHP script / framework as well:
<?php
date_default_timezone_set('Europe/Istanbul');
Well done, you have configured your server's date as accurately as possible!