Wordpress and strange server time behaviours
I recently had to deal with a strange Wordpress bug on a live environment hosted by a german hosting company. Every time I wanted to save a change in the theme customizer, the change was scheduled as a "future post". The publish button changed to "Schedule" after saving the settings. After that, it was no longer possible to make any further changes inside the theme customizer. When trying to reload the customizer, only the error message "Your scheduled changes just published" appeared on the page. Or in german language "Deine geplanten Änderungen wurden gerade veröffentlicht".
What happened here?
First, I pulled the entire Wordpress installation onto a local test system. There I found that the problem did not occur. I then installed a completely new Wordpress version (5.8.2) on the live server without plugins and with default themes. In this new installation the problem occured imidiatelly again. From this I concluded that there was something wrong with the hosting configuration.
Let's take a closer look at the database
I uploaded an Adminer script to the server instance to inspect the database. Every time this error occurs, a future post appears in the database, which means that the customizer can no longer be used. If I delete this post the customizer will be available again but the problem persists.
As you can see in the picture above, a future post was created that describes the actual configuration changes.
Let's look for the cause in the code
First I analyzed the network traffic of the browser to find out what happens when the publish button is clicked by a user. It turns out that the admin-ajax.php file processes the request and an action called "customize_save" is called. At this point in the script I saw a post being saved by the script. This in turn is taken over by the wp-includes/post.php file in the wp_insert_post() method. And this method in turn calls the get_gmt_from_date() function, which is defined in wp-includes/formatting.php. This is where the problem arises. Wordpress transforms all post with a future date into a planned future post. But why the script calculates the wrong time?
As I have read, the problem described can also be related to incorrect Wordpress configurations. You can change the time zone in the settings. In some cases this can solve the problem. But not in this case.
Something is wrong with the server time zone conversion
It turned out that something was wrong with the time zone conversion. So I wrote a small PHP tool that makes the error visible. Note: At this point it has nothing to do with Wordpress. This code works completely independently of Wordpress. Copy this to your server and call it via your browser to verify the problem:
<?PHP
// Create a new DateTime Object from 'now'
$date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
// echo the current time
echo 'php time Europe/Berlin: '.print_r($date,true);
// echo some space
echo '<br>';
echo '<br>';
// Set the timezone to UTC
$date->setTimezone( new DateTimeZone( 'UTC' ) );
// echo the current time
echo 'php time UTC: '.print_r($date,true);
As you can see, both times are the same on the tested system. It shouldn't be like that:
In summer time, Berlin is two hours ahead of UTC. Only one in winter. That is not the case here. Both times are exactly the same. This leads to incorrect time conversion in Wordpress and to inconsistencies in the database.
How can you fix this?
At this point I can't tell you more than that you should contact your hoster. I reported the problem to the affected hoster, but have not yet received an answer. Lets see how it goes on.
Title image: https://pixabay.com/de/photos/zeit-uhr-zeiger-ziffernblatt-3143537/