Rest the month and I keep giving the same month with Carbon - laravel

Until yesterday it worked perfectly showed me February and January, the two previous months but now it is showing me March and January:
This is the date of my computer recently:
This is the code:
I do not understand what is happening if some information is missing or they need something else I can provide it

When subtracting a month from the 29th of March, you would expect to get the 29th of February, which does not exist. So, PHP compensates this by counting the extra days. This way, the date becomes the 1st of march.
To avoid this problem, use the carbon constructor and instruct it to get the last month:
$lastMonth = new Carbon('first day of last month');

Related

How to set Slack reminder for last day of every month?

I need to complete timesheet of work until midnight of every last day of month. Since it's easy to forget, I want to set reminder on say 1PM of the last day. What's the correct syntax to configure it?
All my attempts either don't work or are clumsy:
/remind me "Complete timesheet!!!" at 13:00 on the last day of every month - sets 1st instead and does not completely understand
/remind me "Complete timesheet!!!" at 13:00 on the 31st of every month - works for 31-day-long months, ignores for shorter months
/remind me "Complete timesheet!!!" at 13:00 on the 30st of every month - (I guess) works every month except February, for 30-day-long months one can manually snooze by day
/remind me "Complete timesheet!!!" at -11:00 on the 1st of every month - sets 1st instead and does not completely understand
/remind me "Complete timesheet!!!" 11 hours before 1st day of every month - sets 1st instead and does not completely understand
12x copy of /remind me "Complete timesheet!!!" at 13:00 every November 30th for literally mentioned last day of every month - works but too lengthy
I will also accept answer with link to any documentation with formalized syntax of /remind command (BNF, railroad diagram) from which the possibility of such command would be either provable or disprovable.

Setting reminder for last day of the month

Is there a Slack reminder for last day of the month so that the reminder will occur on the 31st, 30th, or the 28th depending on the month?
Does something like /remind #channel to do X on the last day of the month work?
This syntax seems to work in setting a reminder for the last day of every month:
/remind #someone [What] on the 31st of every month
There is a way but i could not find the best one to do it:
/remind #someone [What] on the 28th of every month
This will remind you on the 28th day of every month. Unfortunately there is no way of working this around. You can set a reminder for every 1st day of the month too:
/remind #someone [What] every month
You can set a reminder for every month but its not that easy to manage:
/remind #someone [What] on the 31st of every January
and so on.
I've just created 12 reminders for myself for the next year
/remind #yourname to "Fill time-report" at 9:00 on Sep 30
/remind #yourname to "Fill time-report" at 9:00 on Oct 31 # Note that October has 31 days
...
This way you can adjust for last weekday of the month being a holiday in your country

Laravel Carbon find same day of week in previous month

I am trying to find the same day of week from last month if today's date is Wednesday Oct 2nd 2019. I need to retrieve Wednesday Sept 4th 2019.
I am using Carbon and have tried subDays(30) and subMonth(1) but that obviously doesn't return the same week day.
SalesLogs::loadByDate(Carbon::now()->subMonth(1));
This code works as expected, however I am unable to work out how to make it find the same day of the week based on the prior month.
It's not super clear what you are trying to do, but I will take a shot at it. What about if you subtract a month, and then go to the next matching weekday?
$weekday = now()->dayOfWeek;
SalesLogs::loadByDate(now()->subMonth(1)->next($weekday));
Note: you can take advantage of Laravel's handy now() helper function, which is equal to Carbon::now(), but saves you from having to import Carbon.
Does that get you what you need?

Complex Date Range

I'm working on the following query and cant figure out the final piece of it. I need my query to give me a result set between the previous business and the previous business day minus (-) 28 days. (e.g. date range between 10/28/2015 and 10/28/2015 -28) The query that I wrote so far is only giving me the -28th day (09/30/2015) and NOT a range in between the previous business day and the previous business day -28. My research shows a couple of different ways of doing it and so far none have worked for me.
SELECT SMBL, SUM(NET_FLOWS/1000000.00)
FROM HISTORY
WHERE DATE - 28 = DATE AND DATE = TO_DATE('10282015','MMDDYYYY')
AND SYMBOL IN ('AAA','BBB')
GROUP BY SMBL
First off, date ranges are easy using BETWEEN, so you do the quick solution:
WHERE DATE BETWEEN (SYSDATE-28) and (SYSDATE-1)
Then you realize your dates have time components, so to include all of yesterday and all of day-28 you need to:
WHERE DATE >= TRUNC(SYSDATE)-28
AND DATE < TRUNC(SYSDATE)
Then I look at your rule "previous BUSINESS day" and ask - what are your business days? On a Monday to go up to the previous Friday? Or Saturday? Or are you a 7-day-a-week business? How about statutory holidays? And is it 28 CALENDAR days back? Or 28 BUSINESS days?
Ahh business rules. The devil is always in those details....

How do I calulate week of year in Oracle using a non-standard first day of the week?

I have a query that needs to return the "week of year" of a date field but the customer of the query uses a non-standard first day of the week so TO_CHAR with 'IW' isn't returning the expected result. In this case the first day of the week is Saturday and Friday is the seventh day of the week.
With T-SQL I'd use DATEPART and SET DATEFIRST.
What is the Oracle equivalent? The Oracle answers I've found in the google all talk about setting the NLS_TERRITORY like so ALTER SESSION SET NLS_TERRITORY = 'UNITED KINGDOM'; but I'm not seeing where I can pick an arbitrary day (other than perhaps finding a territory that uses Saturday).
IW works with a Monday - Sunday week, so this should get you what you are looking for. Basically, get the week according to the day 2 days from now:
to_char(your_date + 2, 'IW')
Oracle's default week format calculates the week number from the first day of the year instead of the first day of the week.
So if a year starts on 01-jan-2009 and the first day is on Wednesday, the week No. 1 will be from 01-jan-2009 to 08-jan-2009 (wednesday to tuesday).
You can use the "iw" format (and a little tweak) if you need the week range to start from sunday through saturday. http://download-uk.oracle.com/docs/cd/B14117_01/server.101/b10749/ch9sql.htm#CIHGFJEI
Try this code below. I basically use the "IW" format and add a condition to get the week number to start from a given date.. say...01-jul-2008.
select target_date,
to_char(target_date+1,'iw') week_sun_thru_saturday,
to_number(to_char(target_date+1,'iw')) -
to_number(to_char( to_date('10-jul-2008','dd-mon-yyyy')+1,'iw')) week_from_01_jul_2008
from t;
Remember..This code will not give week number 1 from jul1st to jul-07 .unless of course 01-jul-2008 is a sunday ;)

Resources