Adding 1 hour to time variable

后端 未结 10 1211
南笙
南笙 2020-12-05 06:27

I have a time to which I want to add an hour:

$time = \'10:09\';

I\'ve tried:

$time = strtotime(\'+1 hour\');

strtotime(\'+1          


        
相关标签:
10条回答
  • 2020-12-05 07:03

    Beware of adding 3600!! may be a problem on day change because of unix timestamp format uses moth before day.

    e.g. 2012-03-02 23:33:33 would become 2014-01-13 13:00:00 by adding 3600 better use mktime and date functions they can handle this and things like adding 25 hours etc.

    0 讨论(0)
  • 2020-12-05 07:04

    try this it is worked for me.

    $time="10:09";
    $time = date('H:i', strtotime($time.'+1 hour'));
    echo $time;
    
    0 讨论(0)
  • 2020-12-05 07:05

    Simple and smart solution:

    date("H:i:s", time()+3600);
    
    0 讨论(0)
  • 2020-12-05 07:08

    Worked for me..

    $timestamp = strtotime('10:09') + 60*60;
    
    $time = date('H:i', $timestamp);
    
    echo $time;//11:09
    

    Explanation:

    strtotime('10:09') creates a numerical timestamp in seconds, something like 1510450372. Simply add or remove the amount of seconds you need and use date() to convert it back into a human readable format.

    $timestamp = strtotime('10:09') + 60*60; // 10:09 + 1 hour
    $timestamp = strtotime('10:09') + 60*60*2; // 10:09 + 2 hours
    $timestamp = strtotime('10:09') - 60*60; // 10:09 - 1 hour
    

    time() also creates a numerical timestamp but for right now. You can use it in the same way.

    $timestamp = time() + 60*60; // now + 1 hour
    
    0 讨论(0)
提交回复
热议问题