我想制作每周一次的表格,我需要在html标记中附加日期。到目前为止,我得到了这段代码:
<table class="schedule-table">
<tr class="days-tr">
<td>
<span><?php echo $date = date("dS",strtotime('monday this week')).' - '.date("dS",strtotime("sunday this week")); ?> </span>
<span> <?php echo $date = date("M-Y",strtotime('monday this week')); ?></span>
</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('monday this week')); ?>">Monday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('tuesday this week')); ?>">Tuesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('wednesday this week')); ?>">Wednesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('thursday this week')); ?>">Thursday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('friday this week')); ?>">Friday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('saturday this week')); ?>">Saturday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('sunday this week')); ?>">Sunday</td>
</tr>
</table>
这是输出:
<table class="schedule-table">
<tbody>
<tr class="days-tr">
<td>
<span>16th - 15th </span>
<span> Mar-2015</span>
</td>
<td data-workout-day="16-Mar-2015">Monday</td>
<td data-workout-day="10-Mar-2015">Tuesday</td>
<td data-workout-day="11-Mar-2015">Wednesday</td>
<td data-workout-day="12-Mar-2015">Thursday</td>
<td data-workout-day="13-Mar-2015">Friday</td>
<td data-workout-day="14-Mar-2015">Saturday</td>
<td data-workout-day="15-Mar-2015">Sunday</td>
</tr>
</tbody>
</table>
正如您所看到的那样需要从下周开始,而不是2015年3月9日?如何解决这个问题才能正确显示? (例如,如果我们有星期五13日至3月,我希望星期一,星期二,星期日的日期不是以下。
答案 0 :(得分:0)
strtotime
非常强大,可以接受很多字符串来获得所需的日期。在这种情况下,您希望this week
获取此周的日期。
通过更改&#39;下周一&#39;等星期一星期一&#39;你得到当周的星期一的日期:
<table class="schedule-table">
<tr class="days-tr">
<td>
<span><?php echo $date = date("dS",strtotime('this week')).' - '.date("dS",strtotime("this week sunday")); ?> </span>
<span> <?php echo $date = date("M-Y",strtotime('monday this week')); ?></span>
</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week monday')); ?>">Monday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week tuesday')); ?>">Tuesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week wednesday')); ?>">Wednesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week thursday')); ?>">Thursday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week friday')); ?>">Friday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week saturday')); ?>">Saturday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week sunday')); ?>">Sunday</td>
</tr>
</table>
生成:
<table class="schedule-table">
<tbody>
<tr class="days-tr">
<td>
<span>09th - 15th </span>
<span> Mar-2015</span>
</td>
<td data-workout-day="09-Mar-2015">Monday</td>
<td data-workout-day="10-Mar-2015">Tuesday</td>
<td data-workout-day="11-Mar-2015">Wednesday</td>
<td data-workout-day="12-Mar-2015">Thursday</td>
<td data-workout-day="13-Mar-2015">Friday</td>
<td data-workout-day="14-Mar-2015">Saturday</td>
<td data-workout-day="15-Mar-2015">Sunday</td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
如果你假设你的星期从星期一开始
然后首先检查
$day = date('l')
if($day != 'Monday'){
echo $date = date("d-M-Y",strtotime( "previous monday" ));
}