0

Нужно вывести вчера сегодня завтра именно в таком формате

06.05.2021 07.05.2021 08.05.2021

$d = date( "d" );
$m = date( "m" );
$Y = date( "Y" );
$d1 = date( "d" )+1;
$d2 = date( "d" )-1;

<div class="sh">0$d2.$m.$Y</div>
<div class="sh">$d.$m.$Y</div>
<div class="sh">0$d1.$m.$Y</div>

Чую, что этот код далеко не идеальный. С нолём перед датой дня проблемы.

1
  • 2
    $tomorrow = date('d m Y', strtotime(strtotime(date('now')).' +1 day')); $yesterday = date('d m Y', strtotime(strtotime(date('now')).' -1 day')); Просто работать со временем надо, а не добавлять нули
    – InDevX
    7 мая 2021 в 14:10

1 ответ 1

1

Сейчас всё чаще используют класс DateTime / DateTimeImmutable:

$datetime = new DateTimeImmutable();

$format = 'd.m.Y';

echo
  $datetime->modify('-1 day')->format($format) . "\n" .
  $datetime->format($format) . "\n" .
  $datetime->modify('+1 day')->format($format) . "\n";
7

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.