views:

856

answers:

4

I get the time:

$today = time();
$date     = date('h:i:s A', strtotime($today));

if the current time is "1:00:00 am", how do i add 10 more hours to become 11:00:00 am??

+1  A: 

$date = date('h:i:s A', strtotime($today . ' + 10 hours'));

(untested)

jensgram
+3  A: 

strtotime() gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 10 hours = 60*60*10 = 36000, so...

$date = date('h:i:s A', strtotime($today)+36000); // $today is today date

Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:

$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already
Amber
+1 for a more elegant solution than adding `+ 10 hours` to `strtotime` :)
jensgram
$today = time();echo date('h:i:s A', strtotime($today)+36000);resut: 10:00:00 am, my localtime is 3.13pm now
myphpsql00
@myphpsql00: That's because `time()` returns a time value in seconds already. Not entirely sure why you're passing `time()` to `strtotime()` - just use `time()` w/o `strtotime()` instead if you're just using the current time. I assumed (because I only glanced at the OP) that you had an actual string time in $today.
Amber
what if i had specific date and time? lets say 28 october 2009, 1:00:00 am? $date = date('h:i:s A', date('28-10-2009 1:00:00')+36000);??
myphpsql00
nvr mind.. I solved the problem, using your method. Thanks Dav
myphpsql00
A: 
$date     = date('h:i:s A', strtotime($today . " +10 hours"));
Marek Karbarz
+3  A: 
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.

see DateTime Class (PHP 5 >= 5.2.0)

RC