PHP - Strtotime - add hours

I have this variable:

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

I just want to add three hours and repeat it.

I saw a way that you can use the 60 * 60 * 3 method or the + 3 hours hardcode where it understands the words.

What is the best way to get this result?

+6
source share
7 answers
 $timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60) 

3*60*60 is the best way

+5
source

The best way is what you think is more readable. The following expressions are identical:

 time() + 3 * 60 * 60 strtotime('+3 hours') 
+12
source

I always love this

 $current_time = date('Ymd 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('Ymd H:i:s', $new_time); echo $new_time; 
+4
source
 $time = new DateTime("+ 3 hour"); $timestamp = $time->format('YMd h:i:s a'); 

Clear and concise :)

+1
source

You can use DateTime::modify to add time, but I would just do time()+10800 .

0
source

If you want to switch to "modern":

 $d = new DateTime(); $d->add(new DateInterVal('P3H')); $timestamp = $d->format('YMd h:i:s a'); 

refs: DateTime object

0
source

Just add seconds to add hours:

 strtotime($your_date)+2*60*60 

This will add two hours to your date.

0
source

Source: https://habr.com/ru/post/918312/


All Articles