How to get the next month in PHP - php

I need to get the next month with php. Everywhere is written these example
date('Y-m-t', strtotime('+1 month'));
The output of the above code is '2017-03-31'. I need to get February, not March.

If you want to get the next irrespective of the current date in the current month. below code may help you
echo date('M',strtotime('first day of +1 month'));
// e.g. "Jan"
echo date('m',strtotime('first day of +1 month'));
// e.g. "1"
echo date('F',strtotime('first day of +1 month'));
// e.g. "January"
This will give you Next month.
You can find more formatting masks in date() function documentation

Use Like This
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));

To get next month using php use string to time function as below:
$nxtm = strtotime("next month");
echo date("M", $nxtm);

date("m", strtotime("2021-08-16 +1 Month"));

You can use the code below to get the next months first day.
$date = (new \DateTime('first day of next month'))->format('Y-m-d');
This is safer than 'next month' or '+1 month' because you may skip some months then.

If you want to have the next month with the same day as this month, or the last day of the next month if it otherwise would switch month you can use this function
function nextMonth()
{
$nextMonthNumber = date('M', strtotime('first day of +1 month'));
$nextMonthDate = new DateTime();
$nextMonthDate->add(new DateInterval('P1M'));
while ($nextMonthDate->format('M') != $nextMonthNumber) {
$nextMonthDate->sub(new DateInterval('P1D'));
}
return $nextMonthDate;
}

Related

Is there a way to get the x-th day of the next month in PHP?

I need to get the 4th day of the next month in PHP. Should be able to generate the date using strtotime()
I followed https://www.php.net/manual/en/datetime.formats.relative.php
tried strtotime('first day of next month +4 days') but it returns first day of next month always because of the precendence I think.
Here is a code you can try. It is my hope that this will work for you
<?php
$d1 = date("Y-m-d", strtotime('first day of next month'));
echo date('Y-m-d', strtotime('+3 days', strtotime($d1)));
Thanks.

get last day of the next month in php?

I have a php code as shown below in which I am trying to retrieve the last day of next month.
<?php
date_default_timezone_set('America/Toronto');
echo date('l jS \of F Y h:i:s A');
$next_month_last_day = date('t', strtotime('next month')); // last day of the next month
print_r("\n");
print_r($next_month_last_day); // Line A
?>
Problem Statement:
I am wondering what mistake I have done in the php code above because the code at Line A prints 31. It should be printing 30 because the last
day of next month (April) is 30.
Just use
date('d', strtotime('last day of +1 month'));
You can use last day of next month
$date = new \DateTime('last day of next month');
echo $date->format('Y-m-d');
Another way to go about it is to get the first day of the next month, then use t to format it to the number of days of that month.
echo date("t", strtotime("first day of next month")); // 2021-04-30 10:27:35
I had to manually overwrite today's day so DateTime wouldn't skip a month. But it doesn't matter since you only want to know what the next month is.
$dateTime = new DateTime('now', new DateTimeZone("America/Toronto"));
$dateTime->setDate($dateTime->format('Y'), $dateTime->format('m'), 01);
$nextMonth = $dateTime->modify('+1 month');
echo cal_days_in_month(CAL_GREGORIAN, $nextMonth->format('m'), $nextMonth->format('Y'));
Try This
$lastDay = date('t',strtotime('next month'));
print_r($lastDay);

Get the last day of the last month and the first day of next month in PHP

If i have for example :
'2020-01-01'
I want to have two dates :
-The last day of the prévious month :
'2019-12-31'
-The first day of next month :
'2020-02-01'
I tried to use something like echo date("Y-n-31", strtotime('2020-01-01')); but i don't know.
Thank you.
Use below code:
<?php
$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");
echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month
?>
With Datetime object with user defined date (Custom date)
# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";
$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";
Output:
30.11.2019
01.01.2020
Try this, Use strtotime() for add month or minus month
$date='2020-01-01';
echo date("Y-m-t", strtotime('-1 month'.$date)); //2019-12-31
echo date("Y-m-01", strtotime('+1 month'.$date)); //2020-02-01
strtotime()
this php function convert english textual date-time discription into UNIX timestamp
the first step converts textual date-time into a timestamp
now you have timestamp then use the date() function
-The last day of the prévious month :
echo date("Y-m-d", strtotime("Last day of last month"));
-The first day of next month :
echo date("Y-m-d", strtotime("First day of next month"));
You can use DateTime's method modify() to achieve relative dates:
<?php
$dateOld = new \DateTimeImmutable('2020-01-01');
echo $dateOld->modify("last day of last month")->format('Y-m-d');
echo PHP_EOL;
echo $dateOld->modify("first day of next month")->format('Y-m-d');
Try it here: https://3v4l.org/qALa5
$date='2020-01-01';
echo date("Y-m-d", 'last day of previous month', strtotime($date)));
echo date("Y-m-d", 'first day of next month', strtotime($date)));

Grabbing specific months first date and lastdate in PHP

function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}
is what i have to get the current month's first day date and last day date.
Now i wish to change it alittle, so in both functions they have a param with $month, where $month holds the number of the month I would like to get the first date and last date from.
Example if i do firstOfMonth(1), I would like it to return 2012-01-01 and lastOfMonth(1) it should return 2012-01-31
How can i do this?
Try this:
$date = new DateTime('2012-02-01');
// first day
echo $date->format('d-m-Y');
// t = days in month, minutes one to go to the last.
$date->modify(sprintf('+%d day', $date->format('t')-1));
// Last day
echo $date->format('d-m-Y');
Try this , if you are using PHP 5.3+,
$query_date = '2012-01-01';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');
For finding next month last date, modify as follows,
$date->modify('last day of 1 month');
echo $date->format('Y-m-d');
and so on..

Finding First day of week via php [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Get first day of week in PHP?
Hi,
I want to find first and last date of current week and last week.
Similarly I want to find first and last date of current month and last month.
This has to be done in PHP. Please help.
strtotime is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Here's a function for the first and last day of the week:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
It can probably be adapted to do month as well, but I wanted to get my answer in! :)

Categories