find last day of any weekday in month - php

I am trying to determine if a given date is the last day of that type (Sunday, Monday, Tuesday, etc.) in its month. For example, for March of 2016, the last Friday of the month is the 25th.
Please note I am not trying to figure out what the last day of the month is. Also, 'weekday' in the question refers to any of the 7 days, not just non-weekend days.
I am using excel and php as tags because I can work with either approach.

Just determine how close the given date is to the end of the month. For the given date in A1, use:
=IF(DATE(YEAR(A1),MONTH(A1)+1,0)-A1<7,TRUE,FALSE)

Related

php Datetime considering 1 January always first week of the year?

is there any parameter or configuratión to provide to a DateTime object to not consider week number in iso but taking the first day of january as the first week of the year?
As Datetime works with an ISO rule, the first week of the year is the week with the first thursday.
I need to make some operations with datetimes but i need it to consider the first day of January as the first week of the year, even if its a Sunday.
Is this possible? I tried everything.
Thank you.
Don’t think that is possible with the native DateTime, no - that only follows ISO 8601 rules. You’d have to handle that yourself, by taking the difference between Jan 1st vs Jan 4th as start of the first week into account in all places where you operate with those values.
You should consider using a date library such as Carbon - with the proper locale set, that should be able to do what you want.
https://carbon.nesbot.com/docs/#api-week:
Week methods follow the rules of the current locale (for example with en_US, the default locale, the first day of the week is Sunday, and the first week of the year is the one that contains January 1st)
There is no option to set the start weekday of first week.
You can only use following format for week number of the day:
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
So if you want to calculate week number assuming 1st day of year as first week of year, then you have to make your own function to add 1 if 1st Jan is not Thursday or before.
Taken from my own answer and adjusted for dateTime and the year starting with week 1.
function get_week($date){
$w=(int)$date->format('W');
$m=(int)$date->format('n');
return $w==1?($m==12?53:1):($w>=51?($m==1?1:$w):$w);
}
2016-01-01 is officially week 53, but the function returns 1. Week 1 now has 10 days, till 2016-01-01. Week 2 starts at 2016-01-11
Function below provides week number 01-53 starting 1st of January. Note: in such way the first and the last week of the year could be less than 7 days.
function getWeekNumberStartingJan1(DateTime $date)
{
if ((clone $date)->modify('1 january')->format('W') !== '01') {
if ($date->format('o') !== $date->format('Y')) {
return '01';
}
return sprintf('%02d', (int)$date->format('W') + 1);
}
return $date->format('W'); //year started from Monday
}

PHP strtotime seems to return an erroneous result for first week of the year (no comma) [duplicate]

I am try to get the year and week of year off of a given date in my code:
$dueDate->format('W , Y');
In the code above, duedate is a datetime object with this date value:
December 31, 2018
When I output the format I specified above, I get this:
01 , 2018
Looking at each value separately the function is correct. However, together it is confusing.
It seems to be reading December 31st as the first week because it falls on a Monday, so technically it is right, it is the first week of 2019. In that case though, I would want the year to roll over and read 2019.
How can I resolve this to roll over the year in this case only? Any help is appreciated.
You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:
ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
$dueDate->format('W , o');

PHP datetime->format : Week of Year and Year result in numbers that don't make sense together

I am try to get the year and week of year off of a given date in my code:
$dueDate->format('W , Y');
In the code above, duedate is a datetime object with this date value:
December 31, 2018
When I output the format I specified above, I get this:
01 , 2018
Looking at each value separately the function is correct. However, together it is confusing.
It seems to be reading December 31st as the first week because it falls on a Monday, so technically it is right, it is the first week of 2019. In that case though, I would want the year to roll over and read 2019.
How can I resolve this to roll over the year in this case only? Any help is appreciated.
You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:
ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
$dueDate->format('W , o');

Php check exact date and time with holiday listing based on weekends

Scenario :- I have a list of holidays but the date themselves don't know weather they are week days or week ends. Based on the calculation of date time with holiday listing and weekend/weekdays.
I have to make a list of calculations based on date and time and holidays added by the user dynamically, and each week day does not have any fixed working hours so removing a fixed hour from a date is not possible.
Also the dates are suppose to be calculated based on the year. So if a date in a year is weekday the date will be included and if another year has the same date as weekend the date will be excluded.
Date calculation based on the year and week ends/week days will be smarter.
PHP will be preferred.
Based on the discussion in the comments, the question is about how to find out if a given date on a weekend.
The 'w' formatting flag can be used to determine the day of the week of a given date, which can be checked against 0 or 6 which indicate Sunday or Saturday.
See DateTime::format('w') and be sure to click through to the date() format description see what else can be used!

PHP date() returns odd values

echo date("W",strtotime('2010-01-01'));
This outputs 53. I would expect it to output 1. Can anyone else confirm this behavior, or maybe explain why? I couldn't find a bug report on it.
This isn't a bug at all, it's expected behaviour. From PHP's Date Page:
W: ISO-8601 week number of year, weeks starting on Monday
Jan 1, 2010, fell on a Friday, so its week number would belong to 2009, making it part of the 53rd week of 2009. Jan 4, 2010 would be week 1.
A week which begins in December and ends in January the following year belongs to the year where most of its days lie. Therefore a week with at least 4 days in December is the last week of that year and a week with at least 4 days in January is the first week in the new year.
So... the last week of a year always contains the 28th day of December. If you take date("W") on that day of a given year you always get the correct number of weeks for that year. The other end of that definition is that the 4th day of January always lies in the first week of a year.
It returns the ISO-8601 week number of year.
From Wikipedia:
There are mutually equivalent descriptions of week 01:
the week with the year's first Thursday in it (the formal ISO definition),
the week with 4 January in it,
the first week with the majority (four or more) of its days in the starting year, and
the week starting with the Monday in the period 29 December – 4 January.
Since 2010-01-01 was a Friday, non of the conditions is met.

Categories