# Check weekend between 2 dates

Hi All,

\$date1 =‘2015-06-12 08:00:00’;
\$date2 =‘2015-06-22 08:00:00’;

What I need:

If (Wednesday is occurring during above dates)
{
// do something
}

Regards

``````<?php
\$date1 = new DateTime('2015-06-08 08:00:00');
\$date2 = new DateTime('2015-06-12 08:00:00');

\$first_date = \$date1->format("w");
\$second_date = \$date2->format('w');

if ( \$first_date <= 3  &&  \$second_date >= 3 ) {
echo "Wednesday occurs between " . \$date1->format("l, F j, Y") . ' and ' . \$date2->format("l, F j, Y") . "\n";
} else {
echo "Wednesday doesn't occurs between " . \$date1->format("l, F j, Y") . ' and ' . \$date2->format("l, F j, Y") . "\n";
}
``````

So lets talk mathematically.

If date 1 and date 2 are 7 or more days apart, there is guaranteed to be a wednesday between them. (Unless the world suddenly decides to skip a wednesday)
Otherwise:
If Date1.DayOfWeek → Date2.DayOfWeek Contains ‘3’, there is a wednesday.

My impulse is to say: You already have days_diff defined (to check the first condition), so… the range(Date1.DayOfWeek, Date1.DayOfWeek + Days_Diff), array_mapped to %7, and then in_array check.

This code doesnt work if date 2 is on a monday and date1 is on a tuesday…

Hi guys thank you all

\$date1 =‘2015-06-12 08:00:00’;
\$date2 =‘2015-06-22 08:00:00’;
If (Friday or Saturday is occurring during above dates)
{
// do something
}

I was wondering where the Wednesday came in to play as a weekend.

Scott

lolz you are right but I mentioned as example only

Hi Guys,

``````\$date1 ='2015-06-12 08:00:00';
\$date2 ='2015-06-22 08:00:00';
If (Friday or Saturday is occurring during above dates)
{
// do something
}
``````

Same logic as the Wednesday, except search for 5 or 6, and instead of 7 days being a guaranteed hit, it’s 6.

slight correction; it would be better to array_walk instead of array_map; though the two are nearly interchangeable.

(I got bored.)

``````<?php
\$date1 =new DateTime('2015-06-12 08:00:00');
\$date2 =new DateTime('2015-06-16 08:00:00');
\$diff =  date_diff(\$date1,\$date2);
if(\$diff->days >= 6) { //Mathematically, there will ALWAYS be either a Friday or Saturday in any given span of 6 consecutive days.
//Warkwark
} else {
\$range = range(\$date1->format("w"),\$date1->format("w") + \$diff->days); // [5,6,7,8,9]
array_walk(\$range, function(&\$a,\$b) { \$a = \$a % 7; }); // [5,6,0,1,2]
if(in_array(5,\$range) || in_array(6,\$range)) { // 5 for Friday, 6 for Saturday.
//Warkwark
}
}
``````
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.