46

I have $adate; which contains:

Tue Jan 4 07:59:59 2011

I want to add to this date the following:

$duration=674165; // in seconds

Once the seconds are added I need the result back into date format.

I don't know what I'm doing, but I am getting odd results.

Note: both variables are dynamic. Now they are equal to the values given, but next query they will have different values.

htoip
  • 437
  • 5
  • 19
ADM
  • 1,590
  • 11
  • 36
  • 54

9 Answers9

93

If you are using php 5.3+ you can use a new way to do it.

<?php 
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
  • 20
    `$date = new \DateTime('+'.$interval.' seconds');` – gondo Jan 07 '15 at 10:09
  • 9
    I think it is easier like this: `$date->add(DateInterval::createFromDateString('2 seconds'));` If you want other interval, look here: http://php.net/manual/en/dateinterval.createfromdatestring.php – Pablo S G Pacheco Jan 30 '15 at 19:12
  • 1
    what is the PT and S in PT674165S ? – Andrew Jan 12 '17 at 02:43
  • 1
    @Andrew The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T. http://php.net/manual/en/dateinterval.construct.php – Elzo Valugi Jan 12 '17 at 11:03
  • Ah thanks, by time element you are referring to time on a clock as opposed to days, from a comment in the docs: "To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value)." But why use this and not @PabloSGPacheco's suggested method. His seem's more readable. – Andrew Jan 12 '17 at 16:46
  • Of course, use what you want or what fits your program best – Elzo Valugi Jan 12 '17 at 19:59
  • Relative datetime is imo easier to read (and remember) -- see @gondo comment. – cottton Aug 14 '19 at 16:54
  • I think this is easier to read: `$date = new DateTime(); $date->modify("+ 2 seconds");` – Nick Johnson Oct 01 '19 at 03:28
29

Just use some nice PHP date/time functions:

$adate="Tue Jan 4 07:59:59 2011";
$duration=674165;
$dateinsec=strtotime($adate);
$newdate=$dateinsec+$duration;
echo date('D M H:i:s Y',$newdate);
DiglettPotato
  • 1,654
  • 3
  • 15
  • 19
  • I've been working with php for a while, and it's awesome and everything (well, except for when it [sucks *\[which doesn't matter\]*](https://blog.codinghorror.com/php-sucks-but-it-doesnt-matter/)), but I just can't get used to how much code is required for the simplest of datetime-manipulation tasks, lol – ashleedawg Jul 05 '19 at 17:03
11

Given the fact that $adate is a timestamp (if that's the case), you could do something like this:

$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);
ncuesta
  • 760
  • 4
  • 12
10
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
Kristian
  • 2,071
  • 23
  • 15
5

Do this:

$seconds = 1;
$date_now = "2016-06-02 00:00:00";

echo date("Y-m-d H:i:s", (strtotime(date($date_now)) + $seconds));
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
user1211927
  • 61
  • 1
  • 3
3
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
Jayesh Paunikar
  • 148
  • 1
  • 7
2

I made this example for a timezone, but if you change some parts it may help you out:

$seconds_to_add = 30;
$time = new DateTime();
$time->setTimezone(new DateTimeZone('Europe/London'));
$time2 = $time->format("Y/m/d G:i:s");
$time->add(new DateInterval('PT' . $seconds_to_add . 'S'));
$timestamp = $time->format("Y/m/d G:i:s");
echo $timestamp;
echo '========';
echo $time2;

Result:

2018/06/17 3:16:23========2018/06/17 3:15:53
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
2

It would be easier with DateTime::modify

(new DateTime($str))->modify("+$duration seconds"); //$str is the date in string
Amir MB
  • 3,233
  • 2
  • 10
  • 16
0

I have trouble with strtotime() to resolve my problem of add dynamic data/time value in the current time

This was my solution:

$expires = 3600; //my dynamic time variable (static representation here)
$date = date_create(date('Y-m-d H:i:s')); //create a date/time variable (with the specified format - create your format, see (1))
echo date_format($date, 'Y-m-d H:i:s')."<br/>"; //shows the date/time variable without add seconds/time
date_add($date, date_interval_create_from_date_string($expires.' seconds')); //add dynamic quantity of seconds to data/time variable
echo date_format($date, 'Y-m-d H:i:s'); //shows the new data/time value

font: https://secure.php.net/manual/en/datetime.add.php (consult Object Oriented style too, the Elzo Valugi solution)

(1) https://secure.php.net/manual/en/function.date.php