I'm looking into trying to set up and array that will look something like this:
$dates = array(
[0] => "07/11/2013",
[1] => "14/11/2013",
[2] => "21/11/2013",
[3] => "28/11/2013",
[4] => "05/12/2013",
[5] => "12/12/2013");
I'm willing to use this, but as I want this to reoccur again next year I'd prefer to have PHP do this and enter it into an array for me. I know how to limit it to a specific amount that I want, but I don't know how to add a week onto the current date or specific date if I wanted to start 08/11/2013
for example.
I've had a quick look and I can't seem to find anything that does this.
I just need a script to add a week to the current date, at the moment this is every Thursday, and then add that to the array.
My only problem is I'm not sure how to specify a date, and then add a week every time. I assume a for
loop would be best here.
strtotime does what you need
If you need that 8 times, loop it 8 times. You can make a function with
$start
and$numWeek
to return an array with$numWeeks
+1 values (the start added)Use
DateTime
class. DateInterval and DatePeriod classes were introduced in PHP 5.3.0, so the below solution works for only PHP >= 5.3.0:Demo!
As Glavic notes in the comments below, this can also be done in previous versions of PHP using the
modify()
method:Demo.
You can use
strtotime('+1 week', $unixTimestamp)
for this:Outputs:
DEMO
(format the
date()
call in any way you want to get the format you want).would the
strtotime()
function work here?To create a 5 element array containing today (or this thursday) and the next 4: