2

I need a function that takes an ISO week and an ISO year as parameter and returns the next ISO week and year (and a function that returns the previous ISO week/year).

hakre
  • 193,403
  • 52
  • 435
  • 836
Mad Scientist
  • 18,090
  • 12
  • 83
  • 109
  • Have you tried any of the [date and time related functions](http://de.php.net/manual/en/book.datetime.php) yet? – Gordon Sep 02 '10 at 09:21
  • Do you need something like this: http://stackoverflow.com/questions/3527166/year-and-week-to-date-in-php/3527481#3527481 – Naveed Sep 02 '10 at 09:25
  • @NAVEED that question refers to getting the date from the ISO week, I'm aleady using that solution, but that is a different problem from the one I'm asking now. – Mad Scientist Sep 02 '10 at 10:27

1 Answers1

5

So answer for 4 day so i give it a shot:

<?php

// Start with Timstamp 0, else the seconds might be off
$x = new DateTime("1970-01-01 00:00:00");
$x->setISODate(2010, 2);
echo $x->format("Y-m-d H:i:s"); // 2010-01-11 00:00:00

$x->modify("+1 week");

echo $x->format("Y-m-d H:i:s"); // 2010-01-18 00:00:00

// Now for the next ISO Week
echo $x->format("Y W"); // 2010 03

Hope that helps, else let me know :)

edorian
  • 38,542
  • 15
  • 125
  • 143
  • I'm going in reverse order, and I get: 2013 03, 2013 02, 2012 01, 2012 52, 2012 51 ... how can I get week #1 to be in 2013? – glen-84 Oct 21 '13 at 14:11
  • It seems to work if I change the offset: $x->setISODate(2010, 2, 3); (loosely based on information [here](http://stackoverflow.com/a/9064954/221528)) – glen-84 Oct 21 '13 at 15:02