How do I add hours to an hour

Asked

Viewed 4,843 times

3

Well I have the following hour in a variable:

$x = "10:15";

I have two different questions,

  1. How do I add 30 minutes to this time.

  2. How do I add 1 hour to this time.

Thank you.

  • Have a look at https://packagist.org/packages/nesbot/carbon it’s worth it. Merry Christmas

2 answers

8


To guess an hour you can do like 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 http://php.net/manual/en/function.strftime.php

  • Go to sleep Miguel. It’s late in Portugal.

  • That’s right, by the way how do I add 1 day to a date, (Just so I don’t have to create a new question).

  • @Gumball haha, you’re right

  • That’s right, by the way how do I add 1 day to a date, (Just so I don’t have to create a new question)

  • @Gonçalo does it by the same logic, that’s it: ...strtotime('10:15') + (60 * 60 * 24);

  • @Gonçalo, strtotime returns the one of secs just multiply

  • But how do I, to display the date on a variable? Sorry la a Massada eheh.

  • @Gonçalo later do: strftime('%d - %m - %Y, %G:%i', $time);

  • I get the logic, thank you very much!

Show 4 more comments

0

There’s a simple way that’s:

$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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.