Search code examples
phpdateddev

DDEV - php has the wrong date/time inside container on Windows


I'm using DDev for local development and have one site that I set up where php thinks the date is 2.5 days previously for example currently the time/date on my host machine is:

March 11 2020 , 4:42pm

But when I check a page with this php:

<?php
$date = date('F d Y h:i:s');
echo $date;
?>

I see this date: March 09 2020 12:59

How do I resolve this date issue?


Solution

  • Edit 2020-03-13: Docker Desktop 2.2.0.4 for Windows claims to have fixed this bug.

    Fixed an issue where containers did not synchronize time with the host when the host machine wakes up from the sleep mode. Fixes docker/for-win#4526.

    Original response:

    Is this on Windows? Docker Desktop for Windows has been having trouble in recent releases, especially related to sleeping or hibernating the host computer. The best approach for a workaround seems to be https://thorsten-hans.com/docker-on-windows-fix-time-synchronization-issue - He recommends a PS script (or manually disable/enable time synchronization in Hyper-V admin console):

    # fix-docker-machine-time-sync.ps1
    $vm = Get-VM -Name DockerDesktopVM
    $feature = "Time Synchronization"
    
    Disable-VMIntegrationService -vm $vm -Name $feature
    Enable-VMIntegrationService -vm $vm -Name $feature
    

    But you can typically: ddev poweroff and then ddev start. You can also ddev poweroff and restart docker.

    This is even in the release notes for Docker Desktop for Windows 2.2.0.3.

    In some cases, Docker containers do not synchronize time with the host when the host machine is put to sleep

    See these Docker Desktop for Windows issues:

    And note that Docker has provided a private edge release for testing this, anybody reading this would help everything by testing it. Link to current private edge release. (Remember that switching to Edge from Stable means losing all your Docker containers and volumes, which means losing ddev databases.)