How do I add hours to an hour

3

Well I have the following time in a variable:

$x = "10:15";

I have two different questions,

  • How do I add 30 minutes to this time.

  • How do I add 1 hour to this time.

  • Thank you.

        
    asked by anonymous 24.12.2016 / 00:12

    2 answers

    7

    To add an hour you can do this:

    $timestamp = strtotime('10:15') + 60*60;
    $dataHora = strftime('%d - %m - %Y, %H:%M', $timestamp); // 24 - 12 - 2016, 11:15
    

    To add 30 mins:

    $timestamp = strtotime('10:15') + 60*30;
    $dataHora = strftime('%d - %m - %Y, %H:%M', $timestamp); // 24 - 12 - 2016, 10:45
    

    strtotime returns the number of secs

    You can see more here about the formatting you want link

        
    24.12.2016 / 00:55
    0

    It has a simple way that is:

    $date = date('Y-m-d H:i', strtotime('+30 minutes')); echo $date;

    To add 1 hour:

    $date_1_hora = date('Y-m-d H:i', strtotime('+1 Hours')); echo $date_1_hora;

        
    02.10.2018 / 15:18