35
today 22-05-2011 so it should be 29-05-2011? ( plus 1 week ) 
or
today 22-05-2011 so it should be 15-05-2011? ( minus 1 week ) 

thanks for looking in.

Adam Ramadhan

JohnP
  • 49,507
  • 13
  • 108
  • 140
Adam Ramadhan
  • 22,712
  • 28
  • 84
  • 124

4 Answers4

74

Use strtotime()

echo date('d-m-Y', strtotime("+1 week")); //1 week in the future
echo date('d-m-Y', strtotime("-1 week")); //1 week ago
JohnP
  • 49,507
  • 13
  • 108
  • 140
  • 3
    One should note that this is a very poor method of doing this, `strtotime` returns seconds since epoch, you can simply add or subtract one week in seconds to the time and avoid the slow call to `strtotime`. `$date = date('d-m-Y', time() + 604800);` – HostFission Sep 13 '17 at 08:48
  • @HostFission your solution does not account for Daylight saving time changes, beware – hexYeah Jun 06 '18 at 13:37
  • @hexYeah indeed it does. `time()` returns a unix timestamp, which is seconds since January 1, 1970 12:00:00 AM UTC (The `U` being `Univeral`). It is then up to you to apply any timezone offsets, including taking DST into account. If this is not working for you be sure you have your timezone set correctly. `date` applies the local system timezone, if you wish to work with UTC you need to use `gmdate`. If you are working with [unix timestamps](https://en.wikipedia.org/wiki/Unix_time) that have a timezone offset applied to them, then they are not unix timestamps. – Geoffrey Jun 07 '18 at 18:00
  • @Geoffrey I was not referring to the answer but the comment below it. I felt the need to highlight because it had a +1 and I don't want people to see this it s a valid solution. If we are about to change the time in the following week the `+ 604800` does not cover that or even leap years – hexYeah Jun 07 '18 at 18:52
  • @hexYeah I don't think you're correct. The commenter is just adding a fixed number of seconds to the timestamp. DST is taken into consideration after the timestamp has been modified, it's got nothing to do with adding 604800 seconds to the timestamp. – JohnP Jun 07 '18 at 21:30
  • If there is a DST change in the next week but you use `+604800` you would be off by +/- an hour, which would be wrong if you convert that time stamp to date (d-m-Y 23:00), even worst for a leap year. `strtotime(+/- x weeks);` accounts for that. I've been there... +7*24*60*60 is not reliable – hexYeah Jun 07 '18 at 21:41
  • @hexYeah I am referring to the comment also. You are completely incorrect on this, a UTC unix timestamp is THE standard. DST varies from place to place, even for when it comes into effect, I live in a timezone with DST and have to deal with this also. A unix timestamp is a clean, unmolested scientific standard. See https://stackoverflow.com/questions/21593692/convert-unix-timestamp-to-date-without-system-libs as to how to actually convert from a Unix timestamp to date correctly, taking leap years into account. You can also see the source in glibc to see how timezones are handled. – Geoffrey Jun 07 '18 at 22:03
  • Sorry for wasting your time guys, I was sure I ran into this issue not too long ago. It must not have been exactly what I remember... I will still look it up :P but you make a point... – hexYeah Jun 07 '18 at 23:33
32

You can use the DateTime class to do calendar calculations. For exaple, to add one week, you could use code like this:

$date = new DateTime('22-05-2011');
$date->modify('+1 week');
Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
17

strtotime will handle this.

$pDate = strtotime('22-05-2011 + 1 week');
echo date('d-m-Y',$pDate);

Added: This is if you want to start from a specific date. If you just want 'today' +/- a week', mark JohnP's answer as correct. : )

John Green
  • 13,241
  • 3
  • 29
  • 51
0

In case If you dint want to hard code today's date you can use Carbon class methods of php.

Carbon::now()->subWeek(1);
Carbon::now()->addWeek(1);
swathi_sri
  • 417
  • 4
  • 8