27

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??

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
myphpsql00
  • 441
  • 2
  • 5
  • 7

7 Answers7

60

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
  • 507,862
  • 82
  • 626
  • 550
  • 2
    +1 for a more elegant solution than adding `+ 10 hours` to `strtotime` :) – jensgram Nov 03 '09 at 07:12
  • $today = time(); echo date('h:i:s A', strtotime($today)+36000); resut: 10:00:00 am, my localtime is 3.13pm now – myphpsql00 Nov 03 '09 at 07:13
  • @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 Nov 03 '09 at 07:18
  • 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 Nov 03 '09 at 07:21
  • nvr mind.. I solved the problem, using your method. Thanks Dav – myphpsql00 Nov 03 '09 at 07:26
  • 1
    -1 The only way to do time calculation is `DateTime` as mentioned by @RC. Everything else will get you into trouble eventually. – fresskoma Oct 08 '12 at 13:22
  • You can put $today="now" for computation. – Ravi Shekhar Oct 12 '15 at 07:04
23
$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)

8

You can simply make use of the DateTime class , OOP Style.

<?php
$date = new DateTime('1:00:00');
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
7

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

(untested)

jensgram
  • 31,109
  • 6
  • 81
  • 98
5
$date = date('h:i:s A', strtotime($today . " +10 hours"));
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Marek Karbarz
  • 28,956
  • 6
  • 53
  • 73
4

Full code that shows now and 10 minutes added.....

$nowtime = date("Y-m-d H:i:s");
echo $nowtime;
$date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
echo "<br>".$date;
Mikeys4u
  • 1,494
  • 18
  • 26
0

In order to increase or decrease time using strtotime you could use a Relative format in the first argument.

In your case to increase the current time by 10 hours:

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

In case you need to apply the change to another timestamp, the second argument can be specified.

Note:

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

So, the recommended way since PHP 5.3:

$dt = new DateTime(); // assuming we need to add to the current time
$dt->add(new DateInterval('PT10H'));
$date = $dt->format('h:i:s A');

or using aliases:

$dt = date_create(); // assuming we need to add to the current time
date_add($dt, date_interval_create_from_date_string('10 hours')); 
$date = date_format($dt, 'h:i:s A');

In all cases the default time zone will be used unless a time zone is specified.

Nik
  • 2,885
  • 2
  • 25
  • 25