get next and previous day with PHP

后端 未结 11 776
悲哀的现实
悲哀的现实 2020-11-28 03:44

I have got two arrows set up, click for next day, next two days, soon and previous day, two days ago, soon. the code seem not working? as it only get one next and previous d

11条回答
  •  悲哀的现实
    2020-11-28 04:03

    always make sure to have set your default timezone

    date_default_timezone_set('Europe/Berlin');
    

    create DateTime instance, holding the current datetime

    $datetime = new DateTime();
    

    create one day interval

    $interval = new DateInterval('P1D');
    

    modify the DateTime instance

    $datetime->sub($interval);
    

    display the result, or print_r($datetime); for more insight

    echo $datetime->format('Y-m-d');
    

    TIP:

    If you don't want to change the default timezone, use the DateTimeZone class instead.

    $myTimezone = new DateTimeZone('Europe/Berlin');
    $datetime->setTimezone($myTimezone); 
    

    or just include it inside the constructor in this form new DateTime("now", $myTimezone);

提交回复
热议问题