1

I am looping the days of the current month and I want to display weekdays of the current month as well but i dont know.

//showing the days number of the current month
$currentDays = date('d');
for($i=1;$i<=$currentDays;$i++)
{
  //print the day number
  echo $i.'\n';
}
Gulmuhammad Akbari
  • 1,986
  • 2
  • 13
  • 28

2 Answers2

0

You can use strtotime to get the timestamp of that specific day and then with that get the weekday name.

$days = date( 'd' );
$month = date( 'n' );

for ( $day = 1; $day <= $days; $day++ )
    echo $day, ' is ', date( 'l', strtotime( $month . '/' . $day ) ), '\n';

To get a different weekday name for translations whatsoever, you must build your own array and get it through from there by changing the lowercase L to lowercase W, which will return you a weekday integer.

$weekdays = array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" );
$days = date( 'd' );
$month = date( 'n' );

for ( $day = 1; $day <= $days; $day++ )
    echo $day, ' is ', $weekdays[ date( 'w', strtotime( $month . '/' . $day ) ) ], '\n';

Keep in mind that you are using lowercase D in your days-variable, which means it will only count the days until today. You can use lowercase T to loop through all days of the specified month.

The alternative is to use mktime as shown in another answer related to this question.

In addition, instead of defining weekday names by yourself, you could simply set the locale setting to your choosing and strftime the weekday name (%A).

Community
  • 1
  • 1
ascx
  • 473
  • 2
  • 13
-1

<?php
$current_month = date("M");
$first_day_this_month = date('01');
$last_day_this_month  = date('t');
$st_result = '';
for($i=$first_day_this_month; $i<=$last_day_this_month; $i++ ){
    $day = '';
 $day = date("l", mktime(0, 0, 0, 5, $i, 2015));
    if($day == 'Saturday' || $day == 'Sunday'){continue;}
 else{$st_result = $st_result .','.$i;}
}
echo $st_result ;
?>
  • This answer does not seem to work, and honestly speaking this code is doing way too many unnecessary things to get to the end result. – ascx May 06 '15 at 06:16