3

Possible Duplicate:
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

I'm new to PHP. How can I be able to list all days in a year? (or from a given date?)

What I would like to do is generate a form with all the days in a month, so I can fill in a budget. And it should end by the end of the year, but it should be able to start from a random date.

Any examples on how I should do this? I should also be able to see the name of the date. Example "21 May - Monday " and list all days and months until 2013.

Community
  • 1
  • 1
MelusDecus
  • 81
  • 4
  • 8
  • Try checking out `strtotime()`. [Heres a good tutorial](http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/). – tomb Nov 20 '12 at 07:35
  • Much better tutorial: http://php.net/manual/en/function.strtotime.php :-) – eisberg Nov 20 '12 at 07:50
  • @eisberg IMO the other tutorial is more suited to what the OP wants. – tomb Nov 20 '12 at 07:53
  • try to use jquery date picker for this purpose it might be pretty easier – Akhilraj N S Nov 20 '12 at 07:39

1 Answers1

5

You could use this, but there could be a more elegant way:

$start_date = '2012-06-01'; // Give in your own start date
$start_day = date('z', strtotime($start_date)); // 6th of June
$days_in_a_year = date('z', strtotime('2012-12-31')); // 31th of december

$number_of_days = ($days_in_a_year - $start_day) +1 ; // Add the last of december also
for ($i = 0; $i < $number_of_days; $i++) {
    $date = strtotime(date("Y-m-d", strtotime($start_date)) . " +$i day");
    echo date('d F - l', $date) .'<br />';
}
CE_REAL
  • 384
  • 5
  • 13