Time calculation in php (add 10 hours)?

后端 未结 7 867
悲哀的现实
悲哀的现实 2020-12-03 03:18

I get the time:

$today = time();
$date = date(\'h:i:s A\', strtotime($today));

if the current time is \"1:00:00 am\", how do i add 10 more

相关标签:
7条回答
  • 2020-12-03 03:25
    $date = date('h:i:s A', strtotime($today . " +10 hours"));
    
    0 讨论(0)
  • 2020-12-03 03:28
    $tz = new DateTimeZone('Europe/London');
    $date = new DateTime($today, $tz);
    $date->modify('+10 hours');
    // use $date->format() to outputs the result.
    

    see DateTime Class (PHP 5 >= 5.2.0)

    0 讨论(0)
  • 2020-12-03 03:28

    $date = date('h:i:s A', strtotime($today . ' + 10 hours'));

    (untested)

    0 讨论(0)
  • 2020-12-03 03:34

    Full code that shows now and 10 minutes added.....

    $nowtime = date("Y-m-d H:i:s");
    echo $nowtime;
    $date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
    echo "<br>".$date;
    
    0 讨论(0)
  • 2020-12-03 03:36

    You can simply make use of the DateTime class , OOP Style.

    <?php
    $date = new DateTime('1:00:00');
    $date->add(new DateInterval('PT10H'));
    echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m
    
    0 讨论(0)
  • 2020-12-03 03:46

    In order to increase or decrease time using strtotime you could use a Relative format in the first argument.

    In your case to increase the current time by 10 hours:

    $date = date('h:i:s A', strtotime('+10 hours'));
    

    In case you need to apply the change to another timestamp, the second argument can be specified.

    Note:

    Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

    So, the recommended way since PHP 5.3:

    $dt = new DateTime(); // assuming we need to add to the current time
    $dt->add(new DateInterval('PT10H'));
    $date = $dt->format('h:i:s A');
    

    or using aliases:

    $dt = date_create(); // assuming we need to add to the current time
    date_add($dt, date_interval_create_from_date_string('10 hours')); 
    $date = date_format($dt, 'h:i:s A');
    

    In all cases the default time zone will be used unless a time zone is specified.

    0 讨论(0)
提交回复
热议问题