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');
Related
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.
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');
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;
i want to get the day of the next week with php..i need something like this
If today is monday, i want to display the days (es 18-19-20) of the next week, not of this week..the week start on monday...but it will work also if today is sunday..always the days of the next week..i can't found anything because i tried to use jddayofweek but i don't understand it maybe..i haven't found an italian guide..
I have a table which display the days in stringn form..so sunday monday etc, fir my problem i tried date() function but if i use +1 on the day i will have problem with february for example
Not sure exactly what you're doing, but maybe something like:
echo date("d", strtotime("monday next week"))
This will give you next Monday:
$date = date_create()->modify('monday next week');
echo date_format($date, "d");
I'm preparing a query for mySQL to grab record from the previous week, but I have to treat weeks as Monday - Sunday. I had originally done this:
WHERE YEARWEEK(contactDate) = YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 DAY))
to discover that mySQL treats weeks as Sunday - Monday. So instead I'm parsing getting the begin & end dates in php like this:
$i = 0;
while(date('D',mktime(0,0,0,date('m'), date('d')-$i, date('y'))) != "Mon") {
$i++;
}
$start_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+7), date('y')));
$end_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+1), date('y')));
This works - it gets the current week's date for monday (walking backwards until a monday is hit) then calculates the previous week's dates based on that date.
My question is: Is there a better way to do this? Just seems sloppy, and I expect someone out there can give me a cleaner way to do it - or perhaps not because I need Monday - Sunday weeks.
Edit
Apparently, there is:
$start = date('Y-m-d',strtotime('last monday -7 days'));
$end = date('Y-m-d',strtotime('last monday -1 days'));
That's about a million times more readable. Thank you.
you can use strtotime for this kind of date issues
echo strtotime("last Monday");
(complementing on marvin and Stomped ) Also you can use it this way
echo date('Y-m-d',strtotime('-1 Monday')); //last Monday
echo date('Y-m-d',strtotime('-2 Monday')); //two Mondays ago
echo date('Y-m-d',strtotime('+1 Monday')); //next Monday
strtotime("previous week Monday")
Monday of the previous week.
Marvin's answer is really elegant, although if you really wanted to go for performance you could do it with a little arithmetic. You could derive a formula/method to convert an arbitrary date to "days since some starting point" (probably 01.01.0000) and then the rest of the operations would be easy with that. Getting the day of week from such a number is as simple as subtracting and getting the remainder of a division.
Actually, PHP had a Date class in its PEAR library which did exactly this.
I have to point out for all the readers here there is a big issue with marvin's answer
Here is the catch
The "last Monday" function will return date the "latest" Monday.It will be explained like this , if today is Monday, then it will return the date of "LAST WEEK" Monday, however if today is not Monday, it will return "THIS WEEK" Monday
The Question is request to return "Last Week" Monday. Therefore the result will be incorrect if today is not Monday.
I have solved the issue and wrapped in my Date Time Helper
It will be only one line after you "INCLUDE" the class
$lastWeekMonday = Model_DTHpr::getLastWeekMonday();
Check Here
https://github.com/normandqq/Date-Time-Helper