Kevin.a
Kevin.a

Reputation: 4306

Skip weekends in php date

I have this code :

 for ($i = 2; $i > -3; $i--)
    { 
        $class="";

        if(date('D d-m')==date('D d-m', strtotime($i . ' days ago')))
        { 
          $class=' class="distinct"'; 
        }

        echo '<li'.$class.'>'.date('D d-m', strtotime($i . ' days ago')) . '</li>'.'<br />';

    }

And it outputs this:

Wed 17-05

Thu 18-05

Fri 19-05

Sat 20-05

Sun 21-05

I was wondering how i can skip the weekends, so it only shows the 5 days monday-friday. Any ideas how to exclude this?

Upvotes: 1

Views: 329

Answers (3)

devofash
devofash

Reputation: 328

Update:

for($i = -2; $i <= 2; $i++)
{
    echo date ( 'D d-m' , strtotime ( "$i weekdays" ) ) . '<br>';
}

Will output:

Wed 17-05
Thu 18-05
Fri 19-05
Mon 22-05
Tue 23-05

===================================

Old answer:

$thisMonday = date("d-M-Y", strtotime('monday this week'));

for ($i = 0; $i <= 4; $i++)
{ 
    $class="";

    $date = date('d-M-Y', strtotime("+$i days", strtotime($thisMonday)));

    if(date('D d-m') == date('D d-m', strtotime($i . ' days ago')))
    { 
      $class=' class="distinct"'; 
    }

    echo '<li'.$class.'>'. date('D d-m', strtotime($date)) . '</li>'.'<br />';

}   

Upvotes: 0

Jerodev
Jerodev

Reputation: 33206

You can start by creating a timestamp for monday and adding enough seconds to advance a day five times.

Like so:

$monday = strtotime('last monday');
for ($i = 0; $i < 5; $i++)
{
    echo date('D d-m', $monday) . '<br />'; 
    $monday = $monday + (60 * 60 * 24); // Add a day
}

Upvotes: 1

rbock
rbock

Reputation: 625

I thing getDate() is just what you need. Where you can use 'wday' to check which day of the week it is (from 0 - Sunday, to 6 - Saturday). Example:

for ($i = 2; $i > -3; $i--)
{ 
    $date = getDate(strtotime($i . ' days ago'));
    if($date['wday'] == 6 || $date['wday'] == 0) {
        echo "Weekend!\n";
    }
    else {
        echo "Regular day...\n";
    }
}

And it outputs:

Regular day...                                                                                                                                                                                                                                
Regular day...                                                                                                                                                                                                                                
Regular day...                                                                                                                                                                                                                                
Weekend!                                                                                                                                                                                                                                      
Weekend! 

Upvotes: 1

Related Questions