Andrew
Andrew

Reputation: 2164

How to get previous year's date relative to week of year

How can I get the same day of the same ISO-8601 week number but in the previous year using php?

I am familiar with extracting this soft of information from a time stamp. Is there a built-in way for me to go from day of week, week of year and year to time stamp?

Upvotes: 3

Views: 752

Answers (4)

sephoy08
sephoy08

Reputation: 1114

Try this one:

$date = "2012-11-13";
echo getLastYearWeek($date);
function getLastYearWeek($date)
{
    $last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));

    return $last_year;
}

Or just,

$last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));

Use this for reference.

Upvotes: 1

Kelvin
Kelvin

Reputation: 5307

You can do this with strtotime:

$now = time(); // Mon, 12 Nov 2012
$targetYear = date('Y', $now) - 1; // 2011
$targetWeek = date('\\WW-N', $now); // W46-1
$lastYear = strtotime($targetYear . $targetWeek); // Mon, 14 Nov 2011

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173572

You can use the combination of both date and strtotime like so:

// get the timestamp
$ts = strtotime('today');

// get year, week number and day of week
list($year, $week, $dow) = explode('-', date('Y-W-N', $ts));

// use the "YYYY-WXX-ZZ" format    
$format = ($year - 1) . "-W$week-$dow";
echo date('Y-m-d', strtotime($format)), PHP_EOL;

Upvotes: 3

Works On Mine
Works On Mine

Reputation: 1121

I think you are looking for mktime. have a look here http://php.net/manual/en/function.mktime.php

Upvotes: 0

Related Questions