Create Variable in PHP Equal to Current Time Minus One Hour

前端 未结 9 1261
青春惊慌失措
青春惊慌失措 2020-12-11 15:12

In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?

相关标签:
9条回答
  • 2020-12-11 15:21

    You could use the date_create function along with the date_sub function like I have shown here below: -

    $currentTime = date_create(now());
    $modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
    $liveTime = $modifyTime->format('Y-m-d H:i:s');
    
    0 讨论(0)
  • 2020-12-11 15:24

    Assuming that a timestamp is fine you can use the time function like so

    <?php
    $livetime = time() - 60 * 60;
    
    0 讨论(0)
  • 2020-12-11 15:26

    Current time is equal to time() (current time given in seconds after Unix epoch).

    Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).

    $time_you_need = time() - 60*60;
    
    0 讨论(0)
  • 2020-12-11 15:29

    If you're looking for how to display the time in a human readable format, these examples will help:

    $livetime = date('H:i:s', time() - 3600); // 16:00:00
    $livetime = date('g:iA ', time() - 3600); // 4:00PM
    
    0 讨论(0)
  • 2020-12-11 15:30

    convert your date to strtotime and then subtract one hour from it

    $now =  date('Y-m-d H:i:s');
    $time   = strtotime($now);
    $time   = $time - (60*60); //one hour
    $beforeOneHour = date("Y-m-d H:i:s", $time);
    
    0 讨论(0)
  • 2020-12-11 15:38

    First convert hours into seconds (3600) then use the following:

    $your_date = date('F jS, Y',time() - 3600);
    
    0 讨论(0)
提交回复
热议问题