tags:

views:

454

answers:

6

I would like to add 24 hours to the timestamp for now. How do I find the unix timestamp number for 24 hours so I can add it to the timestamp for right now?

I also would like to know how to add 48 hours or multiple days to the current timestamp.

How can I go best about doing this?

+4  A: 

Use strtotime:

$currentTimestamp = time();
$in24hourstime = strtotime("+24 hours", $currentTimestamp);

(docs here)

The advantage of using strtotime() is that it's trivial to adjust for eg 48 hours, 2 days 3 hours, without calculating the number of seconds directly.

richsage
+2  A: 

Add 24*3600 which is the number of seconds in 24Hours

Soufiane Hassou
+2  A: 

A Unix timestamp is simply the number of seconds since January the first 1970, so to add 24 hours to a Unix timestamp we just add the number of seconds in 24 hours. (24 * 60 *60)

time() + 24*60*60;
Yacoby
A: 

Unix timestamp is in seconds, so simply add the corresponding number of seconds to the timestamp:

$timeInFuture = time() + (60 * 60 * 24);
reko_t
+10  A: 

You probably want to add one day rather than 24 hours. Not all days have 24 hours due to daylight saving time:

strtotime('+1 day', $timestamp);
Álvaro G. Vicario
+1 for revealing an obscure(ish) edge case
Charlie Somerville
The case is not so obscure, since all other code of earlier questions breaks next sunday morning. +1
Boldewyn
A: 

You could use the datetime class as well:

$d = new DateTime();
$d->setTimestamp(time());

add a P eriod of 1 D ay

$d->add(new DateInterval('P1D'));

echo $d->format('c');

see http://www.php.net/manual/en/dateinterval.construct.php

SeanJA