在整个月中循环每月的日子

I am building multi-calendar, I have a horizontal looking interface: enter image description here

I am trying to run the days of the week S,M,T,W,T,F,S throughout the whole month instead of just the first 7 as in the picture.

the function which draw the calendar:

//our case "SUN" 
if(AC_START_DAY=="sun"){
    for($k=0; $k<7; $k++){
        $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
        $list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>';
    }
}
//If we chose Monday as start week.
else{
    if ($first_week_day == 0)   $first_week_day =7;
    for($k=1; $k<=7; $k++){
        if($k==7)   $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8');
        else        $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
        $list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>';
    }
}

The lang file:

$lang["day_0"]      =   "Sunday";
$lang["day_1"]      =   "Monday";
$lang["day_2"]      =   "Tuesday";
$lang["day_3"]      =   "Wednesday";
$lang["day_4"]      =   "Thursday";
$lang["day_5"]      =   "Friday";
$lang["day_6"]      =   "Saturday";

Already defined

$month=sprintf("%02s",$month);
//  define vars
$today_timestamp    =   mktime(0,0,0,date('m'),date('d'),date('Y'));    #   current timestamp - used to check if date is in past
$this_month         =   getDate(mktime(0, 0, 0, $month, 1, $year));     #   convert month to timestamp
$first_week_day     = $this_month["wday"];                              #   define first weekday (0-6)
$days_in_this_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);    #   define number of days in week
$day_counter_tot    =   0; #    count total number of days showin INCLUDING previous and next months - use to get 6th row of dates

Looks like the $lang["day_".$k.""] is just counting the days from 0 to 6.. how can i make is loop untill the end of the month?

NOTE: I tried increasing the $k<7 just more empty blue boxes appear.

Use the loop to the 30/31 day.

And then change this line

$weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');

to

$weekday = mb_substr($lang["day_".$k%7.""],0,1,'UTF-8');

This should give you the day 0 for every sunday.

0 % 7 = 0 (sunday)
1 % 7 = 1 (monday)
...
7 % 7 = 0 (sunday again)
8 % 7 = 1 (monday again)

You can use this code to generate all the days of the current month.

for ($date = strtotime(date('Y-m-01')); $date < strtotime(date('Y-m-t')); $date = strtotime("+1 day", $date)) {
        echo date("l-d", $date)."<br>";
    }

Will print all the days of the current month as follows.

Thursday-01
Friday-02
Saturday-03
Sunday-04
Monday-05
Tuesday-06
Wednesday-07
Thursday-08
Friday-09
Saturday-10
Sunday-11
Monday-12
Tuesday-13
Wednesday-14
Thursday-15
Friday-16
Saturday-17
Sunday-18
Monday-19
Tuesday-20
Wednesday-21
Thursday-22
Friday-23
Saturday-24
Sunday-25
Monday-26
Tuesday-27
Wednesday-28
Thursday-29
Friday-30

Looks like you almost got it right. You only need to slightly modify your code to make it work the way you want it to. You should just change your code to:

$number_of_days_in_the_future = 42; // Here you can put in the number of days for which you want to display the corresponding letter, and based on your screenshot that is 42
//our case "SUN" 
if(AC_START_DAY=="sun"){
    for($i=0; $i<$number_of_days_in_the_future; $i++){
        $k = $i % 7;
        $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
        $list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>';
    }
}
//If we chose Monday as start week.
else{
    if ($first_week_day == 0)   $first_week_day =7;
    for($i=1; $i<=$number_of_days_in_the_future; $i++){
        $k = $i % 7;
        if($k==7)   $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8');
        else        $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
        $list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>';
    }
}

Please note that I only tried to fix your code so it works as you expect it to. There's probably a more elegant solution but I don't really know your full code so I would just be guessing if I tried to offer you another approach.

I hope this will help you.

Cheers