Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Strtotime - Add hours

Tags:

php

strtotime

I have this variable:

$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time ());

I simply want to add three hours and echo it out.

I have seen the way where you can do the 60 * 60 * 3 method or the hard code "+ 3 hours" where it understands the words.

What is the best way of getting this result?

like image 737
TheLettuceMaster Avatar asked Jun 18 '12 02:06

TheLettuceMaster


People also ask

How do you add hours on Strtotime?

PHP strtotime() Function "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") .

How do you add hours to a timestamp?

To add 24 hours to a Unix timestamp we can use any of these methods: Method 1: Convert 24 hours to seconds and add the result to current Unix time. echo time() + (24*60*60);

How can add hours minutes and seconds in PHP?

The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object.


2 Answers

Just add seconds to add hours:

strtotime($your_date)+2*60*60 

This will add two hours in your date.

like image 66
Akhil Gupta Avatar answered Oct 07 '22 12:10

Akhil Gupta


i always do like this

$current_time = date('Y-m-d H:i:s');
$new_time = strtotime($current_time . "+3hours");
echo $new_time;

or

$new_time = mktime(date('H')+3, 0, 0, date('m'), date('d'), date('Y'));
$new_time = date('Y-m-d H:i:s', $new_time);
echo $new_time;
like image 28
s3polz Avatar answered Oct 07 '22 12:10

s3polz