Count next X days without Sunday using Laravel carbon - laravel

I am developing a subscription based application using Laravel. I want to change the state of the user to expire after 75 days of subscribing to the package. I want to exclude Sundays from these 75 days.
Consider a scenario where user's account is verified today and he has only access to the premium functionalities for 75 days(without Sundays). After the 75 days, the user needs to resubscribe to get access to the premium functionalities of the application.
I will then set up a middleware which will check if the user's subscription is expired or not.
I have two scenarios to check for expiration:
Save the expiration date column in the users table.
Verify each user's request based on verified_at (datetime) column and prevent premium access if the user subscription is over more than 75 days without Sundays.
I want to achieve this using Laravel Carbon or any other alternative library/functionality.
After 75 Days from Today(11 June) is August 25 🙅‍♂️
After 75 Days from Today(11 June - Excluding Sundays) is September 07 👈
Reference: https://getcalc.com/75business-days-after-today.htm

If 75 is fixed, than you could easily calculate the number of Sundays in the period, and so then you just need adding those days to the 75:
$period_in_days = 75;
if($user->verified_at->dayOfWeek > 2)
$period_in_days += 11;
else
$period_in_days += 10;
The point is, in 75 days there could either 10 or 11 Sundays, and so in order to decide whether there are 10 or 11, we need to check which day is the first day.
Let's say is Monday the first day, so the 75 days should looks like this
1 - Monday
2 - Tuesday
...
71 - Monday
72 - Tuesday
73 - Wednesday
74 - Thursday
75 - Friday
Let's say is Tuesday the first day, so the 75 days should looks like this
1 - Tuesday
2 - Wednesday
...
71 - Tuesday
72 - Wednesday
73 - Thursday
74 - Friday
75 - Saturday
Let's say is Wednesday the first day, so the 75 days should looks like this
1 - Wednesday
2 - Thursday
...
71 - Wednesday
72 - Thursday
73 - Friday
74 - Saturday
75 - Sunday
So if the first day is not neither Monday and Tuesday, then there will be 11 Sundays (10 + the 1 that appears in the [71- 75]), otherwise there will be only 10 Sundays

I can count next 75 days excluding Sundays using the following PHP function
function Next75Days($StartingDate){
// Count Next 75 Days excluding Sundays
$Days = 75;
$d = new DateTime($StartingDate);
$t = $d->getTimestamp();
// Loop for 75 days
for($i=0; $i<$Days; $i++){
// Add 1 day to timestamp
$addDay = 86400;
// Get date of next day
$nextDay = date('w', ($t+$addDay));
// if it's Sunday, do $i--1
if($nextDay == 0) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
return $d->format('d M Y');
}

Related

DAX number of days

Can you please help how I can return the total number of days for each month in a given quarter?
For example, I already have 92 days for December 2025 but how can I show 92 days for October and November as well?
If you want only a count, you can use measure:
QuarterDays = calculate(countrows(VALUES('Calendar'[Date])), FILTER(ALL('Calendar'), selectedvalue('Calendar'[Year]) = 'Calendar'[Year] && selectedvalue('Calendar'[Quarter]) = 'Calendar'[Quarter] ))

Cron expression isn't working as intended for spring boot

I wan't to have a method run every sunday at 1:30 PM so far I can get thing to run like every 10 minutes or at 1:30 every day, but now specific day
Here is what I have so far
/**
* Fires at 1:30 PM sundauy
*/
#Scheduled(cron = "0 30 13 * 1 ?")
fun sendNotifications() {
}
I think the problem is in the day of month field possibly? or sunday isn't 1 indexed. I see in other con implementations its 0.
the 5th field is used for "month" while 6th field is for "day of week" (see here and here), so your expression ("0 30 13 * 1 ?") will fire your method at "13:30 of every day in January".
weekday names can be given as the first three letters of the English names. in order to schedule a method to run "every sunday at 13:30" this expression can be used:
#Scheduled(cron = "0 30 13 ? * SUN")

D3 js scaleTime to 365 ~ 366 days in different years

d3.timeParse can parse the time format, but no matter how the format is set, the result contains the year, month, day, hour, minute, and second.
Like this: Sat Jan 01 2020 00:00:00
It can't only correspond to the month and day, if i want to use d3.scaleTime () to scale to compare data of different years, how can I implement it?
For example:
===axis=======
x-axis: 12 month 366 day
y-axis: [max(value), mim(value)]
===data=======
data 1: 2016 = [{ date:2016/01/01,value=18},{ date:2016/01/02,value=199},...,{ date:2016/12/31,value=27}] (366 day)
data 2: 2017 = [{ date:2017/01/01,value=58},{ date:2017/01/02,value=9},...,{ date:2017/12/31,value=217}] (365 day)
data 3: 2018 (365 day)
.
.
data n: 2020 (366 day)

Dateadd() like alternative in Visual Fox pro

How can I add year to Date.
I want to add 65 year to date (12\11\1952).
I have tried "12\11\1952" + 65 ,but it is not giving the required value i.e.
12\11\2017.
please suggest how can i achieve this.
When you add an integer to a Date, you are adding days. ie: Date(1952,11,12)+65 adds 65 days to Nov 12th, 1952.
If you add an integer to a DateTime then you are adding seconds. ie: datetime() + 60*60 adds an hour (60 seconds * 60 mins) to now.
To add a year to a date in VFP, you use GoMonth(). To Add 65 years you use 65 * 12 months:
yearsAdded = GoMonth( Date(1952, 11, 12), 12 * 65 )

algorithm for calculating a week # from a date with custom start of week? (for iCal)

I can only find algorithm for getting ISO 8601 week (week starts on a Monday).
However, the iCal spec says
A week is defined as a seven day period, starting on the day of the
week defined to be the week start (see WKST). Week number one of the
calendar year is the first week that contains at least four (4) days
in that calendar year.
Therefore, it is more complex than ISO 8601 since the start of week can be any day of the week.
Is there an algorithm to determine what is the week number of a date, with a custom start day of week?
or... is there a function in iCal4j that does this? Determine a weekno from a date?
Thanks!
p.s. Limitation: I'm using a JVM language that cannot extend a Java class, but I can invoke Java methods or instantiate Java classes.
if (input_date < firstDateOfTheYear(WKST, year))
{
return ((isLeapYear(year-1))?53:52);
}
else
{
return ((dayOfYear(input_date) - firstDateOfTheYear(WKST, year).day)/7 + 1);
}
firstDateOfTheYear returns the first calendar date given a start of week(WKST) and the year, e.g. if WKST = Thursday, year = 2012, then it returns Jan 5th.
dayOfYear returns sequencial numerical day of the year, e.g. Feb 1st = 32
Example #1: Jan 18th, 2012, start of week is Monday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
(18 - 2)/7 + 1 = 3
Answer Week no. 3
Example #2: Jan 18th, 2012, start of week is Thursday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Thursday, 2012) = Jan 5th, 2012
(18 - 5)/7 + 1 = 2
Answer Week no. 2
Example #3: Jan 1st, 2012, start of week is Monday
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
IsLeapYear(2012-1) = false
Jan 1st, 2012 < Jan 2nd, 2012
Answer Week no. 52
Let daysInFirstWeek be the number of days on the first week of the year that are in January. Week starts on a WKST day. (e.g. if Jan 1st is a WKST day, return 7)
Set dayOfYear to the n-th days of the input date's year (e.g. Feb 1st = 32)
If dayOfYear is less than or equal to daysInFirstWeek
3.1. if daysInFirstWeek is greater than or equal to 4, weekNo is 1, skip to step 5.
3.2. Let daysInFirstWeekOfLastYear be the number of days on the first week of the previous year that are in January. Week starts on a WKST day.
3.3. if daysInFirstWeekOfLastYear is 4 or last year is Leap year and daysInFirstWeekOfLastYear is 5, weekNo is 53, otherwise weekNo is 52, skip to step 5.
Set weekNo to ceiling((dayOfYear - daysInFirstWeek) / 7)
4.1. if daysInFirstWeek greater than or equal to 4, increment weekNo by 1
4.2. if daysInFirstWeek equal 53 and count of days on the first week (starting from WKST) of January in the year of inputDate's year + 1 is greater than or equal to 4, set weekNo to 1
return weekNo

Resources