-4

I am very new to PHP and I am trying to make a PHP webpage that prints out every day of the 2013 year like:

January 1, 2013
January 2, 2013
...........
...........
December 31, 2013

I have been playing with different variations of this code below, but I can't get the date to increment more than once.

$date = "2013-01-01";

for ($counter = 0; $counter <= 365; $counter++) {

$newdate = strtotime('+1 day', strtotime($date));
$newdate = date('Y-m-j', $newdate);

echo "$newdate<br>";

}

2 Answers2

1

It's simple, when you are using strtotime( string ), there is the problem.

Other important thing is that you need to add the date to $date or add the $counter on strtotime( originalDate . '+'.$counter.' day')

1 without counter using $date.

<?php

$date = "2013-01-01";

for ($counter = 0; $counter <= 364; $counter++) {

    echo "$date<br>";

    $newdate = strtotime( $date . '+1 day');
    $newdate = date('Y-m-j', $newdate);
    $date = $newdate;

}

?>

2 with counter:

<?php

$date = "2013-01-01";

for ($counter = 0; $counter <= 364; $counter++) {

    $newdate = strtotime( $date . '+'.$counter.' day');
    $newdate = date('Y-m-j', $newdate);


    echo "$newdate<br>";
}

?>
Mufaddal
  • 5,398
  • 7
  • 42
  • 57
Kesymaru
  • 129
  • 3
1

You always use $date variable for calculating the date, but you are not changing it. Your loop will iterate 366 times (it started from 0, and ends with including 365). Also echo the $date first, or you will start from the 2nd of January

$date = "2013-01-01";

for ($counter = 0; $counter < 365; $counter++) {
    echo "$date<br>";
    $date = date('Y-m-j', strtotime('+1 day', strtotime($date)));
}
Marko D
  • 7,576
  • 2
  • 26
  • 38