Modify DateTime instance to next month - php

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.

Related

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');

Get very first week day of a new month in carbon 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;

php DateTime diff - include both dates in range?

I have been using DateTime Diff (in php) to get various settings for pairs of dates - two formatted dates to display, a difference from the date to now (eg "start date was 3 months 2 days ago"), and a length between the two dates ("length is 2 months 3 days").
The problem is that DateTime Diff ignores one of the days so if the start is yesterday and the end is tomorrow, it gives 2 days whereas I want 3 because both dates should be included in the length. If it was just days, I could simply add 1 to the result, but I wanted to use the years/months/days results from the Diff and these are determined at construct.
The only way I have found to get the desired results is to create a DateTime for start and end (to get the formatted dates and the differences). Then take the end DateTime, add 1 day to it, then work out the length.
It's a bit clunky but there seems to be no way to tell DateTime Diff to include both start and end dates in the result.
DateTime encapsulates a specific moment in time. "yesterday" is not a moment but a time range. The same for "tomorrow".
DateTime::diff() doesn't ignore anything; it just provides you the exact difference (in day, hours, minutes a.s.o.) between two moments in time.
If you want to get the diff between "tomorrow" and "yesterday" as "3 days" you can subtract the first second of "yesterday" from (one second after the last second of "tomorrow").
Like this:
// Always set the timezone of your DateTime objects to avoid troubles
$tz = new DateTimeZone('Europe/Bucharest');
// Some random time yesterday
$date1 = new DateTime('2016-07-08 21:30:15', $tz);
// Other random time tomorrow
$date2 = new DateTime('2016-07-10 12:34:56', $tz);
// Don't mess with $date1 and $date2;
// clone them and do whatever you want with the clones
$yesterday = clone $date1;
$yesterday->setTime(0, 0, 0); // first second of yesterday (the midnight)
$tomorrow = clone $date2;
$tomorrow->setTime(23, 59, 59) // last second of tomorrow
->add(new DateInterval('PT1S')); // one second
// Get the difference; it is the number of days between and including $date1 and $date2
$diff = $tomorrow->diff($yesterday);
printf("There are %d days between %s and %s (including the start and end date).\n",
$diff->days, $date1->format('Y-m-d'), $date2->format('Y-m-d')
);

How to set date to nth day of this month when using Carbon of PHP?

From the document page of Carbon library, it is stated that it is possible to use simple words to initialize a Carbon object. For example, Carbon::parse('first day of December 2008') or new Carbon('first day of December 2008')
However, when I am trying to set the date to 10th of this month by the following trials
Carbon::parse('10th day of this month')
Carbon::parse('tenth day of this month')
Carbon::parse('10th of this month')
Carbon::parse('tenth of this month')
Carbon::parse('10 of this month')
Carbon::parse('ten of this month')
All of these fails.
Currently I am able to create my desired Carbon object by Carbon::parse('first day of this month')->addDays(9), but the readability is not good. More code is needed to be read and it is very easy to mistaken as 9th of this month at the first glaze.
So, is there a way I can create the Carbon object with one parse only?
It's a bit ugly, but this should work
Carbon::parse('10th ' . date('M'));

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