How to specific number of dates except sunday and saturday? - php

I need to get an array of dates except sunday and optional saturday by a specific day count... I found a cool thing - dateperiod, witch gives array to me, but how to exclude sunday and saturday?

Try this...
$daterange = new DatePeriod(...);
$weekdays = [];
foreach($daterange as $date){
if ($date->format("N") < 6) array_push($weekdays, $date);
}
$weekdays should now contain every date object that isn't the 6th or greater day of the week (that is, Saturday or Sunday)

As Simon M says, that seems to be the solution to this issue, it has been answered here too Get date range between two dates excluding weekends
It's a bit disconcerting there's not an option on DatePeriod to exclude days.

Related

Modify DateTime instance to next month

Given a DateTime instance initialized as :
$tgtDateDT = new DateTime('now');
which for example equates to 2023-01-30 when formatted as ->format("Y-m-d"),
I want to advance the month to February (for rendering a calendar) where I was hoping to do:
$nextMonth = $tgtDateDT->add(new DateInterval('P1M'));
Which when formatted with ->format("Y-m-d") yields
2023-03-02
So February has 28 days so I understand it may yield an unpredictable result.
So how can I take a date from any day in one month and advance it to say "the first" of the next month - preferably with DateInterval.
For my calendar rendering the new date can be any day in the next month.
Given any day in a month and needing to advance to the first day of the next month (such as flipping to next month on a calendar) the following can be performed:
$tgtDateDT = new DateTime('now');
// implement "startOfMonth"
$tgtDateDT->setDate($tgtDateDT->format('Y'), $tgtDateDT->format('m'),1);
$tgtDateDT->add(new DateInterval('P1M'));
printf ($tgtDateDT);
So 2023-01-30 yields 2023-02-01.

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

I have a date range that comes from MySQL, for example:
2016-01-05 to 2016-01-10. I would like to check if both Saturday and Sunday are inside that date-range, not necessarily consecutive. So far I have found:
function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}
So I would have to loop for every single day in the range to see if it's a weekend day.
Are there any better approaches?
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).
You would not have to check every single day.
I'll try to explain this with just plain English:
First, check if it starts on a weekend. If it does, you're already done. If the date span is more than 6 days, then you can immediately assume that yes, a weekend day is included. If it's 5 days, then the first day has to be on a Monday, otherwise it contains a weekend day. If it's four, it has to be on a Tuesday... and so on.

get the last thursday before a date in PHP

I know there is similar post about how to get the last Thursday in PHP but I don't want to have the last Thursday compare to the current date but the last Thursday compare to a given date.
For example I have a date dd/mm/yyyy and I want the Thursday before this date.
The input is a String ( the format of the string yymmdd) that I want to parse to get the Thursday before this date.
Thanks for your help
//Assumes it's strtotime parsable, you may need to insert
// slashes with your given format eg (and use 4 digit years)
$given=strtotime($dtstring);
//It's just that easy ;)
$thuBefore=strtotime("last thursday",$given);
Note that this will always get last thursday, meaning if the given date is a Thursday, it'll report 7 days earlier (but if the date's a Friday it'll only report one day earlier).
$day = date('w', $yourTime);
$time = $yourTime - ($day > 4 ? ($day - 4) : ($day + 7 - 4)) * 3600 * 24;
Where both $yourTime and $time are Unix-timestamps.
Edit: #Rudu's solution is way more simple, you should stick with that one :)!

Get Last Monday - Sunday's dates: Is there a better way?

I'm preparing a query for mySQL to grab record from the previous week, but I have to treat weeks as Monday - Sunday. I had originally done this:
WHERE YEARWEEK(contactDate) = YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 DAY))
to discover that mySQL treats weeks as Sunday - Monday. So instead I'm parsing getting the begin & end dates in php like this:
$i = 0;
while(date('D',mktime(0,0,0,date('m'), date('d')-$i, date('y'))) != "Mon") {
$i++;
}
$start_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+7), date('y')));
$end_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+1), date('y')));
This works - it gets the current week's date for monday (walking backwards until a monday is hit) then calculates the previous week's dates based on that date.
My question is: Is there a better way to do this? Just seems sloppy, and I expect someone out there can give me a cleaner way to do it - or perhaps not because I need Monday - Sunday weeks.
Edit
Apparently, there is:
$start = date('Y-m-d',strtotime('last monday -7 days'));
$end = date('Y-m-d',strtotime('last monday -1 days'));
That's about a million times more readable. Thank you.
you can use strtotime for this kind of date issues
echo strtotime("last Monday");
(complementing on marvin and Stomped ) Also you can use it this way
echo date('Y-m-d',strtotime('-1 Monday')); //last Monday
echo date('Y-m-d',strtotime('-2 Monday')); //two Mondays ago
echo date('Y-m-d',strtotime('+1 Monday')); //next Monday
strtotime("previous week Monday")
Monday of the previous week.
Marvin's answer is really elegant, although if you really wanted to go for performance you could do it with a little arithmetic. You could derive a formula/method to convert an arbitrary date to "days since some starting point" (probably 01.01.0000) and then the rest of the operations would be easy with that. Getting the day of week from such a number is as simple as subtracting and getting the remainder of a division.
Actually, PHP had a Date class in its PEAR library which did exactly this.
I have to point out for all the readers here there is a big issue with marvin's answer
Here is the catch
The "last Monday" function will return date the "latest" Monday.It will be explained like this , if today is Monday, then it will return the date of "LAST WEEK" Monday, however if today is not Monday, it will return "THIS WEEK" Monday
The Question is request to return "Last Week" Monday. Therefore the result will be incorrect if today is not Monday.
I have solved the issue and wrapped in my Date Time Helper
It will be only one line after you "INCLUDE" the class
$lastWeekMonday = Model_DTHpr::getLastWeekMonday();
Check Here
https://github.com/normandqq/Date-Time-Helper

work out the date of the fourth saturday in the current month

Bit stuck about how to go about this one. Given the current month, I need to to return the date of the fourth saturday of each month.
e.g. This month would be Feb 20th, next would be March 27th.
Thanks
I'm not a PHP coder, however, it seems strtotime is what you're after.
You can use strtotime("fourth Saturday") and it will return the 4th saturday.
Check out the strtotime docs.
EDIT:
Just to make the answer complete, thanks to Tom and Paul Dixon
date('dS F',strtotime('Fourth Saturday '.date('F o')));
You can use strtotime to find "next saturday" based on a starting date. If that starting date is the day before the earliest possible preceding day (21st) we get the answer...
//required year/month
$yyyymm="2009-01";
//find next saturday after earliest possible date
$t=strtotime("next saturday", strtotime("{$yyyymm}-21"));
//here you go!
echo "4th saturday of $yyyymm is ".strftime("%Y-%m-%d",$t)."\n";
Earliest possible 4th repeat of a day in any month is the 22nd (1,8,15,22), last possible 4th repeat is 28th (7,14,21,28).
EDIT: Although it's not clear in the documentation, you can request the "fourth saturday" too - use the zeroth day of the month as the basis:
$t=strtotime("fourth saturday", strtotime("{$yyyymm}-00"));
or omit the basis time and specify the month and year directly:
$t=strtotime("fourth saturday feb 2009");
Tip of the hat to Robin "I'm not a PHP coder" Day for spotting that :)
The earliest date for the fourth Saturday is the 22nd of the month. So look at the 22nd, see what day of the week it is, if it's not Saturday, add one day to the date, and check again, until you find a match (maximum you would have to check is 6 days).
Find the first Saturday of the month, and then add three weeks to that.
If you don't know when the first Saturday is (or, rather, don't know specifically a date corresponding with a day name), you might want to look at the Doomsday algorithm, which I conveniently looked at for another post with a somewhat similar issue.
function fourth_saturday($year, $month)
{
$info = localtime(mktime(0, 0, 0, $month , 1, $year));
return 28 - $info[6];
}
in PHP rather than pseudo code (think requires 5.2)
$date = getdate();
$date-> setDate($date->format('Y'), $date->format('Y'), '1'); // 1st of month.
while ($date->format('w' != 6)
$date->modify("+1 day");
$date->modify("+21 day"); // date is now on the fourth saturday

Categories