1

Using the PHP date() function, or any other underlining function like cal_days_in_month, how would I print each day in a specific month?

Thank ye, thank ye!

I would like some sort of output like this:

(maybe done with a foreach loop?)

...whatever day name starts the month
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday
...ect
until end of month
seanlevan
  • 1,367
  • 3
  • 15
  • 33

3 Answers3

3

I believe the following function that I wrote will do it.

<?php

function calendar ($month, $year)
{
    $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $today = strtotime(date("Y-m-d")); 
    for ($i = 0; $i < $num; $i++)
    {
        $date = strtotime($year . "-" . $month . "-" . ($i + 1));
        if ($today === $date)
        {
            echo ("!!");
        }
        $day=strftime("%A", $date);
        echo (($i + 1) . " " . $day . "<BR>");
    }
}
?> 

EDITED: Now includes request.

Lugubrious
  • 380
  • 1
  • 3
  • 16
1

This one should give you the code to write the required function. There isn't an underlying PHP function that would do this. Grab all Wednesdays in a given month in PHP

Also see official php documentation http://www.php.net/manual/en/class.datetime.php

PHP - List all days in a month in a year

Do you have any code you have tried?

Community
  • 1
  • 1
Simon
  • 33
  • 1
  • 7
1
function printDay($month,$year=FALSE){
    if(!$year){
        $year=date('Y');
    }
     $num = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
     for($day=1;$day<=$num;$day++){

         echo date('l',  strtotime($year.'-'.$month.'-'.$day));
         echo "<br>";
     }
}

printDay(8);
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71