Check if the current date is weekend in PHP always fails - php

Hello all I have two functions. One to check if current day is weekday and if so return a string of the current day. The other to check if current day is weekend and if so return a string of the current day.
For whatever reason the getWeekend() function always return false even if current date is saturday. Please see code below. Maybe I am doing something wrong....
public function getWeekday()
{
date_default_timezone_set('America/New_York');
$today = \date("l");
if ($today == "Monday") {
return "Monday";
} elseif ($today == "Tuesday") {
return 'Tuesday';
} elseif ($today == "Wednesday") {
return 'Wednesday';
} elseif ($today == "Thursday") {
return "Thursday";
} elseif ($today == "Friday") {
return 'Friday';
} else {
throw new \Exception('Not a valid date.');
}
}
public function getWeekend()
{
date_default_timezone_set('America/New_York');
$today = \date("l");
if ($today == "Saturday") {
return "Saturday";
} elseif ($today == 'Sunday') {
return 'Sunday';
} else {
throw new \Exception('Not a valid date.');
}
}

It will surely throw an exception because the date() is returning the value as Friday for today.
You can check the function for Saturday by by setting the $today = 'Saturday'; Or you can try tomorrow it will work perfectly as desired.

Related

How to check if the next day is available before the previous day in blade (Laravel)

I want to check when the store will be open:
if today is Wednesday and the store is close then it will say will
open on Thursday or Tomorrow.
if not Thursday will go to check Friday and so on but the issue is
it will first check Monday as the number of day is 1.
Then if available the message is the store will open on Monday which is not correct.
So I need to check first the days numbers: 4,5,6,7 then if not available check 1,2,3.
I tried with this but doesn't work.
foreach($storedays as $storeday)
{
if($storeday->storeinfo_id == $item->id)
{
$store_open = \Carbon\Carbon::createFromFormat('H:i', $storeday->open_time)->format('H:i');
$store_close = \Carbon\Carbon::createFromFormat('H:i', $storeday->close_time)->format('H:i');
if ($todayTime >= $store_open && $todayTime <= $store_close && ($storeday->available == 1) && ($storeday->day_id == $today))
{
$is_open = 1;
$when_open = 'now';
break;
}
else
{
if(($storeday->available == 1) AND ($storeday->day_id == $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
if($storeday->day_id==$today)
{
$open_at = 'now';
}
else
{
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
}
break;
}
else if(($storeday->available == 1) AND ($storeday->day_id < $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
break;
}
}
}
}

How can I make an if statement that is dependent on the day of the week?

I would like to make some code which has an if statement that uses some code on Sunday to Wednesday and other code Thursday to Saturday.
This is what I am starting with:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
How can I extend this to take the day of the week into account?
Try this:
$t = (int)date("w");
if ($t <= 3) {
echo "Sunday to wedneday";
} else {
echo "Thursday to Saturday";
}
I would do it this way ( for readability, and future modification )
switch(date('l')){
case 'Sunday':
case 'Monday':
case 'Tuesday':
case 'Wednesday':
//code for these days
break;
case 'Thursday':
case 'Friday':
case 'Saturday':
//code for these days
break;
}
It's very easy to see what this does "at a glance", often times that is the most important thing when programing.
This should be your syntax
if (($day == 'Sunday') || ($day == 'Monday') || ($day == 'Tuesday') || ($day == 'Wednesday')) {
#Do something
} else {
#Do something else
}
I'd access the numeric day
$dow = date('w'); // 0 is Sunday
if ($dow <= 3) {
sundayToWednesday();
} else {
thursdayToSaturday();
}

Check if 2 given dates are a weekend using PHP

I have 2 dates like this YYYY-mm-dd and I would like to check if these 2 dates are a weekend.
I have this code but it only tests 1 date and I don't know how to adapt it; I need to add a $date_end.
$date = '2011-01-01';
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
echo "true";
} else {
echo "false";
}
A couple of hints:
date('N') gives you normalised week day you can test against (no need to use localised strings)
Wrap it all in a custom function and you're done
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;
}
}
Merging multiple answers into one and giving a bit extra, you'd come to the following:
function isWeekend($date) {
return (new DateTime($date))->format("N") > 5 ? true : false;
}
Or the long way:
function isWeekend($date) {
if ((new DateTime($date))->format("N") > 5) {
return true;
}
return false;
}
You can use the strtotime(); and date() functions to get the day of the date, and then check if is sat o sun
function check_if_weekend($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if(in_array($day, array('sun', 'sat'))
return true;
else return false;
}

Add current class for future date

I have a list of events that start and begin on different dates.
04.12.2015–03.01.2016
08.01.2016–14.02.2016
26.02.2016–27.03.2016
And I have a piece of code, that adds a current-event class for a single event if its between some dates.
$startDate = get_post_meta('_event_start_date', true);
$endDate = get_post_meta('_event_end_date', true);
$currentDate = current_time( 'mysql' );
if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
return 'current-event';
} else {
return '';
}
This all works fine and does its job, but what I've encountered is that between two events there isn't any dates to compare. For example, if today's date is the 5th of January and the next event is starting at 8th of January, it doesn't add a current-event class to the future event. I guess I'd have to add another elseif statement to the code, but with what should I compare it with?
Just to bring out the case here, that I'd like to add the current-event class for only one future event.
I think this is what you want:
if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
return 'current-event';
} else if($currentDate < $startDate) {
return 'current-event';
} else {
return '';
}
//version 2:
$startDate = get_post_meta('_event_start_date', true);
$endDate = get_post_meta('_event_end_date', true);
$currentDate = current_time( 'mysql' );
$startDays = array(); //assign your start days to this array & sort it as well
if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
return 'current-event';
} else {
foreach($startDays as $day)
{
if($currentDate < $day)
{
/*
this will return current event for the exactly next event (and not for the other next events).
but make sure to sort the dates in the start days array.
*/
return 'current-event';
break;
}else
{
return '';
}
}
}

How to find the difference between campaign dates and filter date

I have 4 dates with me, 2 for campaign one of them is campaign start date and the other one is for campaign end date and the same thing goes for filter dates. Now I want to check how many days my campaign runs in between a given filter start date and filter end date . I am using these conditions but
if ($datecs != FALSE && $datece != FALSE && $datefs != FALSE && $datefe != FALSE) {
if( ($datecs >= $datefs) && ($datece <= $datefe)) {
// campaign start and end date between filterdates
$tot_days = $this->get_days($datecs,$datece);
if($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif (($datecs <= $datefs) && ($datece >= $datefe)) {
//filterdates between campiagn start and end
$tot_days = $this->get_days($datefs,$datefe);
if($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif(($datecs <= $datefs ) && ( $datece <= $datefe ) && ($datece >= $datefs)) {
// campaign end date between filterdates
$tot_days = $this->get_days($datefs,$datece);
if ($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif(( $datecs>= $datefs) && ($datece >= $datefe) && ($datecs <= $datefe )) {
// campaign start date between filterdates
$tot_days = $this->get_days($datecs,$datefe);
if (($tot_days != 'ok')) {
return $tot_days;
} else {
return 0;
}
}
}
datecs is the campaign start date and datece is campaign end date .datefs is the filter start date and datefe is filter end date. And get_days function gets me the number of days this get_days function I am using
function get_days($date1,$date2)
{
if($date2 > $date1)
{
$interval = $date1->diff($date2);
return $interval->days+1;
//we add one because it will not calculate the starting date if we substract.
//It will give the number of days to reach last date.
} else if($date2 = $date1) {
return 1;
}
else
{
return "ok";
}
}
Please Let me know if I am going wrong some where.
I rewrite your get_days function :
function get_days($date1,$date2)
{
if($date2 < $date1)
{
$interval = $date1->diff($date2);
return $interval->days;
} else if($date2 == $date1) {
return 1;
}
else
{
return 0;
}
}
I don't know if that what you want but i found 2 mistakes :
1) you are using = and not == to compare
2) you swapped in the comparaison : it's $date2 < $date1
Also it's better to return 0 is it failed> Try to not mix return type in a same function, it will make your code easier to understand.
This worked for me.
EDIT
In your second condition (second line) You are doing :
if ($datecs >= $datefs) {
get_days($datecs,$datece); }
And then if the function get_days : if ($date2 > $date1)
That means you are doing
if ($datecs >= $datefs)
then if ($datecs < $datefs)
//do some stuff
You should look into using php carbon. It's a great library that has a lot of functionality that could make this simpler and easier to read and understand.
http://carbon.nesbot.com/docs/

Categories