php / mysql在星期三的星期几和星期二的最后一天开始

时间:2021-11-29 07:24:39

I am workin on a schedule script where the start of the week is on Wednesday and the end is Tuesday. I have been researching all over and find how to start the week on wednesday but the last day always ends on saturday or sunday depending on using DAYOFWEEK or WEEKDAY.

我正在制作一个计划脚本,其中一周的开始时间是星期三,结束时间是星期二。我一直在研究,并找到如何在周三开始一周,但最后一天总是在星期六或星期日结束,取决于使用DAYOFWEEK或WEEKDAY。

my code is as is right now for each of the seven days...

我的代码就像七天中的每一天一样......

                <td class="hours" colspan="2">
            <?php $hour3 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DAYOFWEEK(h.date) = 4
                                        ORDER BY e.emp_id ASC");
            while($time3 = mysql_fetch_array($hour3)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time3['timestart']));
                        $ending = date("g:ia", strtotime($time3['timeend']));
                            if ($time3['work'] == '1') {
                                if ($time3['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time3['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time3['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time3['work'] == '0') {
                                    if ($time3['work_type'] == '1') {
                                        if ($time3['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time3['work_type'] == '2') {
                                        if ($time3['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time3['work_type'] == '3') {
                                        if ($time3['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time3['work_type'] == '4') {
                                        if ($time3['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time3['work_type'] == '0') {
                                        if ($time3['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hours" colspan="2">
            <?php $hour4 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DAYOFWEEK(h.date) = 5 
                                        ORDER BY e.emp_id ASC");
            while($time4 = mysql_fetch_array($hour4)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time4['timestart']));
                        $ending = date("g:ia", strtotime($time4['timeend']));
                            if ($time4['work'] == '1') {
                                if ($time4['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time4['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time4['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time4['work'] == '0') {
                                    if ($time4['work_type'] == '1') {
                                        if ($time4['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time4['work_type'] == '2') {
                                        if ($time4['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time4['work_type'] == '3') {
                                        if ($time4['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time4['work_type'] == '4') {
                                        if ($time4['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time4['work_type'] == '0') {
                                        if ($time4['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hours" colspan="2">
            <?php $hour5 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DAYOFWEEK(h.date) = 6 
                                        ORDER BY e.emp_id ASC");
            while($time5 = mysql_fetch_array($hour5)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time5['timestart']));
                        $ending = date("g:ia", strtotime($time5['timeend']));
                            if ($time5['work'] == '1') {
                                if ($time5['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time5['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time5['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time5['work'] == '0') {
                                    if ($time5['work_type'] == '1') {
                                        if ($time5['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time5['work_type'] == '2') {
                                        if ($time5['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time5['work_type'] == '3') {
                                        if ($time5['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time5['work_type'] == '4') {
                                        if ($time5['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time5['work_type'] == '0') {
                                        if ($time5['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hours" colspan="2">
            <?php $hour6 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DATE_ADD((DAYOFWEEK(h.date) = 7), INTERVAL 7 DAY)
                                        ORDER BY e.emp_id ASC");
            while($time6 = mysql_fetch_array($hour6)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time6['timestart']));
                        $ending = date("g:ia", strtotime($time6['timeend']));
                            if ($time6['work'] == '1') {
                                if ($time6['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time6['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time6['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time6['work'] == '0') {
                                    if ($time6['work_type'] == '1') {
                                        if ($time6['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time6['work_type'] == '2') {
                                        if ($time6['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time6['work_type'] == '3') {
                                        if ($time6['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time6['work_type'] == '4') {
                                        if ($time6['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time6['work_type'] == '0') {
                                        if ($time6['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hours" colspan="2">
            <?php $hour0 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DATE_ADD((DAYOFWEEK(h.date) = 1), INTERVAL 7 DAY)
                                        ORDER BY e.emp_id ASC");
            while($time0 = mysql_fetch_array($hour0)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time0['timestart']));
                        $ending = date("g:ia", strtotime($time0['timeend']));
                            if ($time0['work'] == '1') {
                                if ($time0['why_off'] == '1')
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time0['why_off'] == '2')
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time0['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time0['work'] == '0') {
                                    if ($time0['work_type'] == '1') {
                                        if ($time0['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time0['work_type'] == '2') {
                                        if ($time0['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time0['work_type'] == '3') {
                                        if ($time0['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time0['work_type'] == '4') {
                                        if ($time0['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time0['work_type'] == '0') {
                                        if ($time0['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hours" colspan="2">
            <?php $hour1 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DATE_ADD((DAYOFWEEK(h.date) = 2), INTERVAL 7 DAY)
                                        ORDER BY e.emp_id ASC");
            while($time1 = mysql_fetch_array($hour1)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time1['timestart']));
                        $ending = date("g:ia", strtotime($time1['timeend']));
                            if ($time1['work'] == '1') {
                                if ($time1['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time1['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time1['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                                elseif ($time1['work'] == '0') {
                                    if ($time1['work_type'] == '1') {
                                        if ($time1['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                    }
                                    elseif ($time1['work_type'] == '2') {
                                        if ($time1['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                    }
                                    elseif ($time1['work_type'] == '3') {
                                        if ($time1['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                    }
                                    elseif ($time1['work_type'] == '4') {
                                        if ($time1['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                    }
                                    elseif ($time1['work_type'] == '0') {
                                        if ($time1['timeend'] == '23:00:00')
                                        {echo "<div class='working_w'>".$starting." - Close</div>";}
                                            else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                    }
                                }?>

                </div>
                <?php } ?>

            </td>
            <td class="hoursboth" colspan="2">
            <?php $hour2 = mysql_query("SELECT h.* FROM hours AS h, employees AS e 
                                        WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND DATE_ADD((DAYOFWEEK(h.date) = 3), INTERVAL 7 DAY)
                                        ORDER BY e.emp_id ASC");
            while($time2 = mysql_fetch_array($hour2)) { ?>

                <div class="hours">
                    <?php $starting = date("g:ia", strtotime($time2['timestart']));
                        $ending = date("g:ia", strtotime($time2['timeend']));
                            if ($time2['work'] == '1') {
                                if ($time2['why_off'] == '1') 
                                    {echo "<div class='off'>OFF [R]</div>";}
                                elseif ($time2['why_off'] == '2') 
                                    {echo "<div class='off'>OFF [ML]</div>";}
                                elseif  ($time2['why_off'] == '0')
                                    {echo "<div class='off'>OFF</div>";}
                            }
                            elseif ($time2['work'] == '0') {
                                if ($time2['work_type'] == '1') {
                                    if ($time2['timeend'] == '23:00:00')
                                    {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                                        else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                                }
                                elseif ($time2['work_type'] == '2') {
                                    if ($time2['timeend'] == '23:00:00')
                                    {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                                        else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                                }
                                elseif ($time2['work_type'] == '3') {
                                    if ($time2['timeend'] == '23:00:00')
                                    {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                                        else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                                }
                                elseif ($time2['work_type'] == '4') {
                                    if ($time2['timeend'] == '23:00:00')
                                    {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                                        else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                                }
                                elseif ($time2['work_type'] == '0') {
                                    if ($time2['timeend'] == '23:00:00')
                                    {echo "<div class='working_w'>".$starting." - Close</div>";}
                                        else {echo "<div class='working_w'>".$starting." - ".$ending."</div>";}
                                }
                            }?>

                </div>
                <?php } ?>

            </td>

I would like to thank whomever in advance for any help given!

我想提前感谢任何人给予的帮助!

2 个解决方案

#1


0  

how to start the week on wednesday

如何在周三开始这一周

So basically you just want to “shift” a range of values ranging from 0 to 6 by a given number … well that’s really easy:

所以基本上你只是想要“移动”给定数字范围从0到6的一系列值...这很容易:

$shifted_value = ( $actual_value + $offset ) % $number_of_values;

So here $offset would be 4 (Wednesday is day #3 of the week in PHP, but subtracting 3 could lead to unwanted negative values with the modulo division – so we just shift the “into the future” instead of into the past, by adding 4 [=7-3]) and $number_of_values would be 7.

所以这里$ offset将是4(星期三是PHP中每周的第3天,但减去3可能会导致模数除法产生不必要的负值 - 所以我们只是将“转向未来”而不是过去,添加4 [= 7-3])和$ number_of_values将为7。

This would be the PHP version, in MySQL it can be done the same way – only the offset has to be adapted accordingly to the fact that for MySQL Wednesday is day #2 of the week.

这将是PHP版本,在MySQL中它可以以相同的方式完成 - 只需要相应地调整偏移量,因为MySQL周三是本周的第2天。

#2


0  

What about substracting some days from the date and selecting the resulting week?

那么从日期开始减去某些天并选择产生的周数呢?

SELECT h.* FROM hours AS h, employees AS e 
WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND 
    WEEK(DATE_ADD(h.date, INTERVAL -3 DAY)) = 4
ORDER BY h.date, e.emp_id ASC

#1


0  

how to start the week on wednesday

如何在周三开始这一周

So basically you just want to “shift” a range of values ranging from 0 to 6 by a given number … well that’s really easy:

所以基本上你只是想要“移动”给定数字范围从0到6的一系列值...这很容易:

$shifted_value = ( $actual_value + $offset ) % $number_of_values;

So here $offset would be 4 (Wednesday is day #3 of the week in PHP, but subtracting 3 could lead to unwanted negative values with the modulo division – so we just shift the “into the future” instead of into the past, by adding 4 [=7-3]) and $number_of_values would be 7.

所以这里$ offset将是4(星期三是PHP中每周的第3天,但减去3可能会导致模数除法产生不必要的负值 - 所以我们只是将“转向未来”而不是过去,添加4 [= 7-3])和$ number_of_values将为7。

This would be the PHP version, in MySQL it can be done the same way – only the offset has to be adapted accordingly to the fact that for MySQL Wednesday is day #2 of the week.

这将是PHP版本,在MySQL中它可以以相同的方式完成 - 只需要相应地调整偏移量,因为MySQL周三是本周的第2天。

#2


0  

What about substracting some days from the date and selecting the resulting week?

那么从日期开始减去某些天并选择产生的周数呢?

SELECT h.* FROM hours AS h, employees AS e 
WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND 
    WEEK(DATE_ADD(h.date, INTERVAL -3 DAY)) = 4
ORDER BY h.date, e.emp_id ASC