how to get date of yesterday using php?

前端 未结 9 1552
悲&欢浪女
悲&欢浪女 2021-01-30 04:51

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?

相关标签:
9条回答
  • 2021-01-30 05:19

    you can do this by

    date("F j, Y", time() - 60 * 60 * 24);
    

    or by

    date("F j, Y", strtotime("yesterday"));
    
    0 讨论(0)
  • 2021-01-30 05:21

    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');
    
            ?>           
    
    0 讨论(0)
  • 2021-01-30 05:23

    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.)

    0 讨论(0)
  • 2021-01-30 05:26

    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'
    
    0 讨论(0)
  • 2021-01-30 05:28

    Yesterday Date in PHP:

    echo date("Y-m-d", strtotime("yesterday")); 
    
    0 讨论(0)
  • 2021-01-30 05:34

    try this

    <?php
    $yesterday = date(“d.m.Y”, time()-86400);
    echo $yesterday;
    
    0 讨论(0)
提交回复
热议问题