marcinf2
marcinf2

Reputation: 83

Displaying date from monday to sunday

I want to make a table weekly schedule , and I need to attach the dates in html markup. So far I got this code :

    <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>

And here is the output :

<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>

As you can see it takes the Monday date from following week instead of 9-Mar-2015 ? How to fix that to display it properly? (for example if we have Friday 13-Mar i want the mon, tues, thur to have dates from previous week not the following.

Upvotes: 2

Views: 1679

Answers (2)

ajaykumartak
ajaykumartak

Reputation: 776

If you are assuming that you week is starting from monday

then first of all check

$day = date('l')
if($day != 'Monday'){
echo $date = date("d-M-Y",strtotime( "previous monday" ));
}

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14891

strtotime is hugely powerful and can accept a lot of strings to get the desired date. In this case, you want this week to get this weeks dates.

By changing 'next monday' etc to 'this week monday' you get the dates for Monday of this current week:

<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>

Generates:

<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>

Upvotes: 0

Related Questions