Sebastiano
Sebastiano

Reputation: 21

php: advance to next year minus 1 day

Problem: I'm using strtotime to advance by 364 days in the future, but I'm getting troubles with leap years.

Example: today is January 20, 2012 - I need PHP to compute for me the timestamp of January 19, 2013.

If I simply add

strtotime("+364 days");

I correctly get January 18, 2013 - but for the code I'm writing I don't need to consider leap years and thus I expect to obtain January 19, 2013.

Any quick and dirty way to do this?

Upvotes: 1

Views: 1293

Answers (2)

Kartik
Kartik

Reputation: 9873

You can also try checking if the year is leap or not and then add that +/- 1 Day

<?php

function is_leap()
{
    $year = date('Y');
    $check = ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));
    return $check;
}

$year = (is_leap()) : strtotime("+365 days") ? strtotime("+364 days")

?>

Upvotes: 0

rauschen
rauschen

Reputation: 3996

strtotime() takes the leap year into account.

replace strtotime("+364 days"); with strtotime('+1 year -1 day');

note: not everybody want to read a bunch of comments to get the answer

Upvotes: 3

Related Questions