Checking If Date Is Weekend PHP

Checking if date is weekend PHP

If you have PHP >= 5.1:

function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}

otherwise:

function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}

php check if date entered is weekend

Try like

if(date('l', strtotime($date)) == 'Sunday' || date('l', strtotime($date)) == 'Saturday'))))  {
echo 'Event on a weekend';
} else {
echo 'Event is on a weekday';
}

Or even you can try like

if(date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0) {
echo 'Event on a weekend';
} else {
echo 'Event is on a weekday';
}

Check a day to see if it is a weekend day

This will return you 0 to 6, with 0=Sunday, 1=Monday, etc.

$dw = date( "w", $timestamp);

So... to show the notification:

<?php
$dw = date( "w");
if ($dw == 6 || $dw == 0) {
$datetime = new DateTime('today');
if ($dw == 6) {
$datetime->modify('+3 day');
} else {
$datetime->modify('+2 day');
}
echo "Contact us again at: " . $datetime->format('Y-m-d');
} else {
echo "Today is: " . date('l jS \of F Y')."<br/>";
$datetime = new DateTime('today');
$datetime->modify('+1 day');
echo "Tomorrow is: ". $datetime->format('Y-m-d') ."<br/>";
}
?>

Check if 2 given dates are a weekend using PHP

You can use shorter code to check for weekend => date('N', strtotime($date)) >= 6.
So, to check for 2 dates — and not just 1 — use a function to keep your code simple and clean:

$date1 = '2011-01-01' ;
$date2 = '2017-05-26';

if ( check_if_weekend($date1) && check_if_weekend($date2) ) {
echo 'yes. both are weekends' ;

} else if ( check_if_weekend($date1) || check_if_weekend($date2) ) {
echo 'no. only one date is a weekend.' ;

} else {
echo 'no. neither are weekends.' ;
}

function check_if_weekend($date) {
return (date('N', strtotime($date)) >= 6);
}

Using your existing code, which is slightly longer, following is how you would check for 2 dates:

$date1 = '2011-01-01' ;
$date2 = '2017-05-27';

if ( check_if_weekend_long($date1) && check_if_weekend_long($date2) ) {
echo 'yes. both are weekends' ;

} else if ( check_if_weekend_long($date1) || check_if_weekend_long($date2) ) {
echo 'no. only one date is a weekend.' ;

} else {
echo 'no. neither are weekends.' ;
}

function check_if_weekend_long($date_str) {
$timestamp = strtotime($date_str);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
//echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
return true;
} else {
return false;
}
}

How to check if there is a weekend between two dates in php?

You can use date("N") to get the current day of the week and add the difference of days between your dates... If this is bigger or equal to 6 than it's a weekend between or one date is in the weekend.

//Get current day of week. For example Friday = 5
$day_of_week = date("N", strtotime($fromDate));

$days = $day_of_week + (strtotime($toDate) - strtotime($fromDate)) / (60*60*24);
//strtotime(...) - convert a string date to unixtimestamp in seconds.
//The difference between strtotime($toDate) - strtotime($fromDate) is the number of seconds between this 2 dates.
//We divide by (60*60*24) to know the number of days between these 2 dates (60 - seconds, 60 - minutes, 24 - hours)
//After that add the number of days between these 2 dates to a day of the week. So if the first date is Friday and days between these 2 dates are: 3 the sum will be 5+3 = 8 and it's bigger than 6 so we know it's a weekend between.



if($days >= 6){
//we have a weekend. Because day of week of first date + how many days between this 2 dates are greater or equal to 6 (6=Saturday)
} else {
//we don't have a weekend
}

PHP if today is Saturday or Sunday

The issue is, you have to check the condition individually.

Try this:

<?php
if(date('D') == 'Sat' || date('D') == 'Sun') {
echo "Today is Saturday or Sunday.";
} else {
echo "Today is not Saturday or Sunday.";
}
?>

Explanation:

date() function with 'D' parameter will return the day like Sat, Sun etc

Output : Today is Saturday or Sunday.

Working code

date() parameters list

php determine if a range of dates fall on a weekend

$dates = array();
$christmas_days = array(25, 26);

$year = 2013;

for ($day = 25; $day <= 31; $day++)
{
$dates[$day] = date($day . '-12-' . $year);
}



foreach ($dates as $day => $date)
{
echo $day . ' ';
if (isWeekend($date))
{
echo 'weekend';
if (in_array($day, $christmas_days))
{
$bh_on_weekend++;
}
}
else
{
if ($bh_on_weekend > 0)
{
echo 'bh';
$bh_on_weekend--;
}
else
{
if (in_array($day, $christmas_days))
{
echo 'bh';
}
else
{
echo 'leave';
}
}
}
echo '<br>';
}

function isWeekend($date)
{
return (date('N', strtotime($date)) >= 6);
}

tried to do on the DRY way to repeat as less as possible. date creation is a bit more dynamical. hope it helps you somehow or give an idea how to make it different.

output for 2021

25 weekend
26 weekend
27 bh
28 bh
29 leave
30 leave
31 leave

How can I verify that a date range includes a weekend?

You don't need to loop over all the days. You just need to know the length of the date range and the first day's day of the week.

Assuming you have two DateTime objects:

$start = new DateTime('2016-01-05');
$end = new DateTime('2016-01-10');

You can determine if a Saturday and Sunday are contained in the date range by checking if the length of the range plus the numeric weekday of the start date is greater than 6.

function includes_weekend (DateTime $start, DateTime $end) {
return $start->diff($end)->format('%a') + $start->format('w') > 6;
}

format('%a') returns the total number of days in the DateInterval returned by diff, and format('w') returns the numeric day of the week (Sunday = 0).

Checking if date falls on weekday or weekend?

$weekday = date("w", strtotime("2012-01-01"));

This will be 0 for Sunday through 6, Saturday.

http://www.php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.date.php



Related Topics



Leave a reply



Submit