How pass month value and get start and end date of month In laravel - laravel

I am a new Laravel and using Laravel 5.5.
I want to pass value of month and get start and end date of passing month.
Suppose pass 5 (May month) the get value
Start date: 01-05-2019
End date: 30-05-2019
I have tried:
$finalMonth = 5;
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
$endMonth = Carbon::now()->addMonth($finalMonth)->endOfMonth()->format("Y-m-d");
But It's given the wrong result because of it adds current month+pass month.
Means 4+5=9 that means sept.
Please Help

Instead of using:
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
Use instead the month method:
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");
See: carbon-get-month-by-number-returns-march-for-number-2

Use instead Below :
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");

Related

Carbon: comparing date instances is giving unexpected result

This problem is driving me crazy.
Here is my Carbon instance:
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date);
What I want to do is just to understand if the date here is the last day of the week.
$carbonTemp->equalTo($carbonTemp->endOfWeek())
If $date = '2021-08-08' which is the last day of this week, the result above must be true, right?
It is giving me FALSE !!
Even though $carbonTemp->endOfWeek()->format('Y-m-d') is giving me '2021-08-08'.
endOfWeek() returns last day of week and time 23:59:59.999999. Therefore it is necessary to use endOfDay() method when creating date.
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date)->endOfDay();
that's because you created CarbonImmutable object, so endOfWeek will not work.
Create a normal Carbon date:
$carbonTemp = Carbon::createFromFormat('Y-m-d', $date);

Is there a way to make __timeShift only select certain days of the week?

The current function in use in the JMeter script is
${__timeShift(dd/MM/yyyy,${__time(dd/MM/yyyy)},-P31D,,)}
to specify a review date 31 days ago.
I now learn that the project requires the review date to always fall on a Monday. Is there any way to make sure that performing a timeShift only selects a Monday?
I'm afraid __timeShift() function is not flexible enough, you can consider using __groovy() function instead and implement the following algorithm:
Get current date
Subtract 31 day
If current day of week is Monday - return the date in the past
Otherwise add 1 day until the date in the past becomes Monday
Example function code:
${__groovy(def now = new Date(); def monthAgo = now.minus(31); while (monthAgo[Calendar.DAY_OF_WEEK] != Calendar.MONDAY) { monthAgo = monthAgo.plus(1)}; return monthAgo.format('dd/MM/yyyy'),)}
Demo:
More information: Apache Groovy - Why and How You Should Use It

Getting records for the previous calendar month

I am trying to get the records of the previous month. That is say we are in February - I'd want to get records from 1st January to 31st January.
I tried:
$approved_reviews_lastMonth = ReviewHeader::where('created_at', '=', Carbon::now()->subMonth(1))->count();
But this does not get any for the last month.
Anyone?
Use format() to get the Year-Month format, and use like to get the previous month count.
$approved_reviews_lastMonth = ReviewHeader::where('created_at', 'like', Carbon::now()->subMonth(1)->format("Y-m")."%")->count();
You can use whereBetween as well.
$firstDayofPreviousMonth = Carbon::now()->startOfMonth()->subMonth()->toDateString();
$lastDayofPreviousMonth = Carbon::now()->subMonth()->endOfMonth()->toDateString();
$approved_reviews_lastMonth = ReviewHeader::whereBetween('created_at', [$firstDayofPreviousMonth,$lastDayofPreviousMonth])->count();

Year, Month, and Day parameters describe an un-representable DateTime Exception

I'm adding an object to a list within an ASP.NET MVC 3 application using the following code but one of the properties of the object is giving me difficulties.
ls.Add(new UserRoleModel { UserRoleId = 1, UserName = "Paul", InsertDate = new DateTime(05, 24, 2012),InsertProgram="sqlplus",InsertUser="sp22",Role="Implementation Co-corindator"});
This builds but when I go to the relevant page i get an Exception with the following type:
Exception Details: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
I've tried removing the 0 in the month but I get the same exception.
The DateTime constructor you are calling accepts parameters in the order year, month, day.
You are providing them in the order month, day, year -- which ends up trying to assign meaningless values. The documentation is pretty clear on what the allowed values are and what happens if you pass 2012 for the "day" value.
I just ran into this and my issue was I was creating a date in February. I tried to do the following...
new Date(2013, 2, 30)
Since there is not a February 30th, the date failed to create. When I changed to
new Date(2013, 2, 28)
it worked fine.
if the InsertDate meant to be the date / time of creation you can just use the following
DateTime InsertDate = DateTime.Now;
You are using an Invalid datetime, like for month you may be passing 13,14 days more than 31
or something like that which is causing the issue.
i was having the same issue because i was trying to create a DateTime object with date set to 31 of April even though April only has 30 days.
Anyone having the same issue, make sure you are not making this mistake.
Simply you have to change the InsertDate into
InsertDate = new DateTime(Year,Month,Date);
It will solve your problem.
I was facing the same type of issue, where I was using a DateTimePicker in Winform Application. Where user can only pick only month and year and my code and UI was look like
Code
this.dtpMonth.CustomFormat = "MMMM yyyy";
this.dtpMonth.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.dtpMonth.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dtpMonth.Location = new System.Drawing.Point(88, 24);
this.dtpMonth.Name = "dtpMonth";
this.dtpMonth.ShowUpDown = true;
this.dtpMonth.Size = new System.Drawing.Size(136, 22);
this.dtpMonth.TabIndex = 1;
And I was initializing the by this.dtpMonth=DateTime.Today; and this line cost me and error of current discussion.
Afterward, I have found that my date(DateTime.Today) was 31 July. So, when user want change the month to August it throws exception because August doesn't have date 31. Finally, I change my code into
dtpMonth.Value =new DateTime(DateTime.Today.Year, DateTime.Today.Month,1);
And worked for me.
The same exception I solved as:
DateTimePicker1.Value = DateSerial(Year(Now), Month(Now) + 1, 1)
For those who has this issue, make sure that date, which you try to create exists. For example, I had this problem, because was trying to create 29 feb at not leap year
if you got this error because you're trying to insert 29 days in a year that's not a leap year, then you can check the maximum allowed days first of that year as such:
if(DateTime.DaysInMonth(2024,2) == 29)
dt = new DateTime(2024, 2, 29);

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources