Bluemagica
Bluemagica

Reputation: 5158

How do I find a date in php using timestamps?

I have a timestamp, say $now, I need to find the timestamp for, say, 45 days after the date of $now. What do I do?

Upvotes: 0

Views: 68

Answers (6)

Fabrizio D'Ammassa
Fabrizio D'Ammassa

Reputation: 4769

This should give what you need:

$new_ts = $now+45*24*60*60;

Upvotes: 0

aparker
aparker

Reputation: 178

from http://www.php.net/manual/en/datetime.add.php :

$a = new DateTime("now");
var_dump($a);
date_add($a, date_interval_create_from_date_string("45 days"));
var_dump($a);

object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2011-08-24 09:00:03"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2011-10-08 09:00:03"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}

Upvotes: 0

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

I prefer using the (PHP 5 >= 5.2.0) DateTime functions:

$iTimeStamp = time();
$oDateTime = DateTime::createFromFormat("U", $iTimeStamp);
$oDateTime->add(new DateInterval('P45D'));
echo $oDateTime->getTimeStamp();

Upvotes: 0

ajreal
ajreal

Reputation: 47321

you can use strtotime

$the_45_days_later = strtotime('+45 day', $now);

the precision of the timestamp is not rounded to 00:00:00,
but followed exactly the time that $now belong to

the other answers has show using pure seconds calculation,
which is precise too

$the_45_days_later = $now+(86400*45);

Upvotes: 1

Petr Peller
Petr Peller

Reputation: 8836

1 day has 86400 seconds so the code is:

$now = time();
$after = $now + 45*86400;

Upvotes: 0

genesis
genesis

Reputation: 50974

                                 //sec min  hour  day
$fourty_five_days_after = time() + 60 * 60 * 24 * 45;

Upvotes: 0

Related Questions