Mawg
Mawg

Reputation: 40140

Calculate the time stamp of year

Given that I have $num_years, how can I add it to Time() to get a time stamp $num_years hence?

Upvotes: 0

Views: 150

Answers (6)

Starx
Starx

Reputation: 78941

Well if you looking for the timestamp for just that time then use mktime

mktime(0,0,0,0,0,$noOfYears);

http://www.php.net/manual/en/function.mktime.php

Upvotes: 2

Fabio Mora
Fabio Mora

Reputation: 5479

echo strftime('%Y %a %d %m, %T', strtotime('+' . $num_years . ' years') );

Change strfttime format according your needs. Check out strtotime man page http://php.net/manual/en/function.strtotime.php

Upvotes: 2

Angus
Angus

Reputation: 1350

strtotime("+$num_years years") will give you a timestamp $num_years years in the future.

Upvotes: 2

Haim Evgi
Haim Evgi

Reputation: 125446

is this what you want ?

//Example to add 1 year to a date object
$num_years = 1;

$currentDate = date("Y-m-d");// current date

//Display the current date
echo "Current Date: ".$currentDate."<br>";

//Add one year to current date
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +$num_years years");
echo "Date After adding one year: ".date('l dS \o\f F Y', $dateOneYearAdded)."<br>";

Upvotes: 1

Michal M
Michal M

Reputation: 9480

Is this what you're after?

$num_years = 5;
$future = strtotime("+{$num_years} years");

Upvotes: 2

Andreas Wong
Andreas Wong

Reputation: 60506

try

echo strtotime("now + $num_years years");

Upvotes: 2

Related Questions