Get very first week day of a new month in carbon php - php

Hello please i am having problem in getting the start week date of next month using php carbon.
I try the below code with out the start of the current month not the added month.
$carbon = new Carbon();
$thisMonth = $carbon::now()->startOfMonth();
$nextMonth = $thisMonth->addMonth();
echo $nextMonth->startOfWeek();
Out put of the above code
2020-09-28 00:00:00
Expectation:
Today is 27 - 09 2020 i expected to output the first week day of the next month with is 01 - 10 -2020.
I need to get the first date (Monday - Friday) of every next month from the current month when execute this code.

If you're trying to get First Date of Every Next Month, you can try this:
now()->addMonthNoOverflow(1)->firstOfMonth()->dayName;

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 to add a year to today's date and default it to the preceding working day

If I want to default a date to the next working day I am using the following:
<?php
echo(date('d/m/Y',strtotime('+1 Weekdays')));
?>
For example: If a user is adding an item on a Friday it is given a default of the following Monday - the next working day.
I have to create a schedule of events with a start and end date. The end date needs to 1 year in the future on the preceding working day.
For example: If a user adds a schedule that has a start day of Wednesday and the same date in a years time happens to be a Sunday, then the end date needs to default to the previous Friday - the preceding working day.
I found the answer:
<?php
echo(date(date('d/m/Y',strtotime('+1 year')),strtotime('-1 Weekdays')));
?>
You just need to add one year to today's date then check the day of the week, if it is 'Sat' or 'Sun' subtract one weekday. The PHP DateTime object makes this easy with the format() and modify() methods.
$inOneYear = new DateTime('+1 year');
if(in_array($inOneYear->format('D'), ['Sat', 'Sun'])){
$inOneYear->modify('-1 weekday');
}
echo $inOneYear->format('D, d/m/Y');
In all these cases:
today (Thursday, Dec. 1st, 2022)
tomorrow (Friday, Dec. 2nd, 2022)
the next day (Saturday, Dec. 3rd, 2022)
the above will output:
Fri, 01/12/2023
The strtotime() function and the DateTime constructor both take stacking relative values, and will process them in order, so you can do things like:
$when = strtotime('now +1 year -1 weekdays');
$when = new DateTime('now +1 year -1 weekdays');

How to get the starting weekday of a month in a future year using date() in php

I am using a date() format to return the starting weekday of a month. The code I have below is how I am attempting to achieve this. For the current year (2018) this works as normal. For example This month is august and the starting weekday is a Wednesday so it will return a 3 for Wednesday. (It works so far)
As we advance the year to 2019 it starts to get the starting weekday wrong.
For example January 2019 starts on a Tuesday so it should return 2 but returns 1. (one day out)
This error seems to be cumulative so if we go to 2020 then it is 2 days out etc.
I have tried so hard to format this Date() correctly but to no avail. Is this even the correct way to do this?
Code:
$future_month = 5 /*for January 2019*/
$starting_weekday = date('N',mktime(0, 0, 0, date('m', strtotime('+'.$future_month.' months', strtotime(date('Y-m-01')))), 1));
Many Thanks
Cameron
Your code makes this much more complicated than it needs to be.
$dt = new DateTime('first day of +5 months')
$dt->format('N'); // "2"

PHP - Why create DateTime with relative format of year does not work

I don't understand why create a date for last or next year works:
new DateTime('first day of January last year');
But not for another year :
new DateTime('first day of January 2 years ago');
I got:
Exception: DateTime::__construct(): Failed to parse time string (first day of January 2 year ago) at position 23 (y): The timezone could not be found in the database
Or maybe I don't write it correctly. Nothing in the PHP docs on the page of relative format say that does not work with year.
Anyone has a explanation?
You have the order wrong. This works:
new DateTime('2 years ago first day of january');
DateTime is an exceptionally picky class.
new DateTime('first day of January +2 years'); // works fine
new DateTime('first day of January -2 years'); // doesn't work at all
In the second case, changing the position of the predicates gets the right result:
new DateTime('-2 years first day of January'); // 2015-01-01
This is also the case for your example string:
echo (new DateTime('2 years ago first day of january'))->format('Y-m-d');
// 2015-01-01
This may or may not be useful, depending on where that string is coming from.
To avoid any ambiguity, or just for clarity, it can sometimes be better to break this down into two operations:
new DateTime('first tuesday this year'))->format('Y-m-d'); // 2017-11-28 (what?)
new DateTime('january first tuesday'))->format('Y-m-d'); // 2017-01-24 (nope)
$dt = (new DateTime('first day of January'))->modify('next tuesday');
echo $dt->format('Y-m-d'); // 2017-01-03 (Woohoo!)

How can I get name of month after the current month?

The following gives me October
echo date('F');
I'm using the following code to give me the name of the coming month (the month after this one), so I'm hoping to see November.
$nextmonth = date("F",strtotime("+1 months"));
Today is 31st Oct, but the above gives 'December' What am I doing wrong? How can I get the month after the current month?
Use relative formats to get the first day of the next month:
echo (new DateTime('first day of next month'))->format('F');
The reason why you see this problem is when adding time to a date at the end of the month you run into issues with months having fewer than 31 days. This can cause you to skip a month. The best bet is to always start your date math at the beginning of the month before adding time to it or just relative formats as demonstrated above.
PHP 5.3:
$nextMonth = new DateTime('first day of next month');
echo $nextMonth->format('F');

Categories