how do I subtract 24 hour from date time object in PHP

后端 未结 10 1361
我寻月下人不归
我寻月下人不归 2020-12-17 07:57

I have the following code:

  $now = date(\"Y-m-d H:m:s\");
  $date = date(\"Y-m-d H:m:s\", strtotime(\'-24 hours\', $now));

However, now it

相关标签:
10条回答
  • 2020-12-17 08:01

    You can simply use time() to get the current timestamp.

    $date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
    
    0 讨论(0)
  • 2020-12-17 08:09

    This may be helpful for you:

    //calculate like this
    $date = date("Y-m-d H:m:s", (time()-(60*60*24)));
    
    //check the date
    echo $date;
    
    0 讨论(0)
  • 2020-12-17 08:09
    $now = date("Y-m-d H:i:s");
    $date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));
    

    Add "strtotime" before $now, and Y-m-d H:m:s replace with Y-m-d H:i:s

    0 讨论(0)
  • 2020-12-17 08:12

    this should work, too

    $date = date("Y-m-d H:m:s", strtotime('-24 hours'));
    
    0 讨论(0)
  • 2020-12-17 08:18

    you can do this in many ways...

    echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros
    

    OR

    echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)
    

    Output

    2013-07-17 10:07:29
    
    0 讨论(0)
  • 2020-12-17 08:21

    Simplest way to sub or add time,

    <?php
    **#Subtract 24 hours**
    $dtSub = new DateTime('- 24 hours');
    var_dump($dtSub->format('Y-m-d H:m:s'));
    **#Add 24 hours**
    $dtAdd = new DateTime('24 hours');
    var_dump($dtAdd->format('Y-m-d H:m:s'));die;
    ?>
    
    0 讨论(0)
提交回复
热议问题