PHP - Add Two Hours to Date Variable

Add 'x' number of hours to date

You may use something like the strtotime() function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.

How to Add Hours with Variable to a date time Variable

This was a simple solution:

$newDate = date('Y-m-d H:i:s', strtotime($menue_datum. ' + ' . $ts3 . 'hours'));

adding two time values of similar formats using php

this code sample would take hour in $time and add the hour in $time2 to it

for example: time=06:58:00, time2=00:40:00, result = 07:38:00

$time = "06:58:00";
$time2 = "00:40:00";

$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);

Add one hour to PHP variable with date

There's quite a few ways to do this with PHP. Here's one using DateTime():

$datetime = new DateTime('07/05/2016');
$datetime->modify('+1 hour');
echo $datetime->format('Y-m-d H:i:s');

In your case you can also just add the literal time since there is no time for that date and midnight is assumed:

echo '07/05/2016 01:00:00';

Just for fun, here are a few more ways to do it:

// Using DateInterval()
$datetime = new DateTime('07/05/2016');
$datetime->add(new DateInterval('PT1H'));
echo $datetime->format('Y-m-d H:i:s');

// As a one-liner
echo (new DateTime('07/05/2016'))->->modify('+1 hour')->format('Y-m-d H:i:s');

PHP add up two time variables

The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:

$o = strtotime($time1)+strtotime($time2);

If I remember right strtotime does support this format.

Otherwise you will need to filter it out yourself.

in PHP 5.4 How do I add 2 hours to a UTC date variable (and return UTC formatted for ical)

Try this:

$rightNowUTC = gmdate("Ymd\THis\Z");  //time right now
$endDateForWelcome = gmdate("Ymd\THis\Z", strtotime('+ 2 hours'));

$endDateForWelcome is a string so you have to parse it with strtotime()

http://php.net/manual/en/function.strtotime.php

echo "Time Right Now: ". $rightNowUTC ."</br>";
echo "Time After Adding:". $endDateForWelcome;

how to add two time more than 24 hours

You have a logical error in your code, you just simply sum every part of the time but the thing you need to do is this :

If number of seconds is superior to 60 you have to get only the result of $number_seconds%60 and add it to your minutes and repeat it with the minutes, let me show you :

       function TimeSum($a,$b)
{
list ($hour1, $min1, $sec1) = explode(':', $a);
list ($hour2, $min2, $sec2) = explode(':', $b);
//counting number of seconds and getting extra minutes outs
$total_sec = $sec1+$sec2;
$sumSec = $total_sec%60;
$extra_min = ($total_sec-$sumSec)/60;
//counting number of minutes and getting extra hours outs
$total_min= $min1+$min2+$extra_min;
$sumMin = $total_min%60;
$extra_hr = ($total_min-$sumMin)/60;
//counting number of hours
$sumHour = $hour1 + $hour2 + $extra_hr;

return $sumHour.':'.$sumMin.':'.$sumSec;
}


Related Topics



Leave a reply



Submit