Subtract 1 day with PHP

后端 未结 10 732
我在风中等你
我在风中等你 2020-11-28 06:52

I\'m trying to take a date object that\'s coming out of my Drupal CMS, subtract one day and print out both dates. Here\'s what I have

$date_raw = $messageno         


        
相关标签:
10条回答
  • 2020-11-28 07:23

    How to add 1 year to a date and then subtract 1 day from it in asp.net c#

    Convert.ToDateTime(txtDate.Value.Trim()).AddYears(1).AddDays(-1);
    
    0 讨论(0)
  • 2020-11-28 07:26

    Answear taken from Php manual strtotime function comments :

    echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));
    

    Or

    $date = "2009-01-31";
    echo date( "Y-m-d", strtotime( $date . "-1 day"));
    
    0 讨论(0)
  • 2020-11-28 07:34

    A one-liner option is:

    echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');
    

    Running it on Online PHP Editor.


    mktime alternative

    If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:

    // Today's date
    echo date('Y-m-d'); // 2016-03-22
    
    // Yesterday's date
    echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21
    
    // 42 days ago
    echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09
    
    //Using a previous date object
    $date_object = new DateTime('2011-04-24');
    echo date('Y-m-d',
      mktime(0, 0, 0,
         $date_object->format("m"),
         $date_object->format("d")-1,
         $date_object->format("Y")
        )
    ); // 2011-04-23
    

    Online PHP Editor

    0 讨论(0)
  • 2020-11-28 07:39
     date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
    
    0 讨论(0)
提交回复
热议问题