how to get date of yesterday using php?

2020-05-11 16:17发布

I want to get the yesterday date using specific date format in php this is the format:

$today = date("d.m.Y"); //15.04.2013

Is it possible?

Take consideration of month and years if they should be changed in respective.

标签: php date
8条回答
我命由我不由天
2楼-- · 2020-05-11 16:41

You can also do this using Carbon library:

Carbon::yesterday()->format('d.m.Y');         // '26.03.2019'

In other formats:

Carbon::yesterday()->toDateString();          // '2019-03-26'
Carbon::yesterday()->toDateTimeString();      // '2019-03-26 00:00:00'

Carbon::yesterday()->toFormattedDateString(); // 'Mar 26, 2019'
Carbon::yesterday()->toDayDateTimeString();   // 'Tue, Mar 26, 2019 12:00 AM'
查看更多
地球回转人心会变
3楼-- · 2020-05-11 16:43

try this

        $tz    = new DateTimeZone('Your Time Zone');
        $date  = new DateTime($today,$tz);
        $interval = new DateInterval('P1D');
        $date->sub($interval); 

        echo $date->format('d.m.y');

        ?>           
查看更多
唯我独甜
4楼-- · 2020-05-11 16:44

there you go

date('d.m.Y',strtotime("-1 days"));

this will work also if month change

查看更多
霸刀☆藐视天下
5楼-- · 2020-05-11 16:44

Yesterday Date in PHP:

echo date("Y-m-d", strtotime("yesterday")); 
查看更多
Luminary・发光体
6楼-- · 2020-05-11 16:51

If you define the timezone in your PHP app (as you should), which you can do this way:

date_default_timezone_set('Europe/Paris');

Then it's as simple as:

$yesterday = new DateTime('yesterday'); // will use our default timezone, Paris
echo $yesterday->format('Y-m-d'); // or whatever format you want

(You may want to define a constant or environment variable to store your default timezone.)

查看更多
家丑人穷心不美
7楼-- · 2020-05-11 16:54

try this

<?php
$yesterday = date(“d.m.Y”, time()-86400);
echo $yesterday;
查看更多
登录 后发表回答