问题
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?
回答1:
$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60)
3*60*60 is the best way
回答2:
The best way is what you think is more readable. The following expressions are identical:
time() + 3 * 60 * 60
strtotime('+3 hours')
回答3:
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;
回答4:
$time = new DateTime("+ 3 hour");
$timestamp = $time->format('Y-M-d h:i:s a');
Clear and concise :)
回答5:
You can use DateTime::modify to add time, but I would just do time()+10800.
回答6:
If you want to go 'modern':
$d = new DateTime();
$d->add(new DateInterVal('P3H'));
$timestamp = $d->format('Y-M-d h:i:s a');
refs: DateTime object
回答7:
Just add seconds to add hours:
strtotime($your_date)+2*60*60
This will add two hours in your date.
来源:https://stackoverflow.com/questions/11076334/php-strtotime-add-hours