Hướng dẫn php date 1 month

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time[] variable works, but I am not sure how to +1 month onto that?

Nội dung chính

  • How to Add one month to current date in PHP?
  • How to get date month in PHP?
  • How can I get current date from next month?
  • How can I get first date and date of last month in PHP?

Any ideas, suggestions. Cheers!

asked Jun 6, 2014 at 12:11

Try this

$today = date["Y-m-d"];
$date = date['Y-m-d', strtotime['+1 month', $today]];

or use DateTime[]

$dt1 = new DateTime[];
$today = $dt1->format["Y-m-d"];

$dt2 = new DateTime["+1 month"];
$date = $dt2->format["Y-m-d"];

answered Jun 6, 2014 at 12:12

Ing. Michal HudakIng. Michal Hudak

5,09011 gold badges58 silver badges91 bronze badges

1

$time = strtotime["2010-12-11"];
$final = date["Y-m-d", strtotime["+1 month", $time]];

[OR]

strtotime[ "+1 month", strtotime[ $time ] ];

this returns a timestamp that can be used with the date function

answered Jun 6, 2014 at 12:12

vinothinivinothini

751 silver badge8 bronze badges

Use this:

Current Date:

echo "Today is " . date["Y/m/d"];

1 Month to the Current Date:

$time = strtotime[date["Y/m/d"]];
$final = date["Y-m-d", strtotime["+1 month", $time]];

answered Jun 6, 2014 at 12:14

Anand SolankiAnand Solanki

3,3994 gold badges15 silver badges27 bronze badges


answered Jun 6, 2014 at 12:29

shorter : $today=date["Y-m-d"]; $date=

answered Oct 9, 2016 at 8:12

1

This one liner worked for me:

$monthFromToday = date["Y-m-d", strtotime["+1 month", strtotime[date["Y/m/d"]]]];

answered May 5, 2021 at 20:50

The given answers may not give you the results you might expect or desire.

Consider:

$today = "29Jan2018";
$nextMonth = date['dMY', strtotime['+1 month', [strtotime[$today]]]];
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018

atline

24.6k16 gold badges59 silver badges93 bronze badges

answered Oct 8, 2018 at 23:36

0

$today = date["Y-m-d"];
$enddate = date['Y-m-01',strtotime[$today. ' + 1 months']];

Daniel

9,00611 gold badges49 silver badges64 bronze badges

answered May 10, 2021 at 17:06

You could also consider using the Carbon package.

The solution would look like this:

use Carbon\Carbon

$now = Carbon::now;
$now->addMonth[];

Here is the link for reference //carbon.nesbot.com/docs/

answered May 10, 2021 at 17:14

TijnvdEijndeTijnvdEijnde

851 gold badge1 silver badge6 bronze badges

2

How to Add one month to current date in PHP?

Example 1: PHP Add Months to Date.

index.php. $date = "2021-11-01"; $newDate = date['Y-m-d', strtotime[$date. ' + 3 months']]; ... .

Output: 2022-02-01..

index.php. $newDate = date['Y-m-d', strtotime[' + 4 months']]; echo $newDate; ?>.

Output: Read Also: How to Add Days to Date in PHP? 2022-03-01. i hope it can help you....

How to get date month in PHP?

There are several methods to get the month portion of a date in PHP..

Using the date[] Function to Get the Current Month of a Date in PHP..

Using strtotime[] and date[] Functions to Get the Current Month of a Date in PHP..

Get the Current Month by Using the DateTime Class in PHP..

How can I get current date from next month?

one way of doing this is- first getting last date of current month and then adding 1 day to it, to get next month's first date.

How can I get first date and date of last month in PHP?

You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime['February 2012']; $first_second = date['m-01-Y 00:00:00', $timestamp]; $last_second = date['m-t-Y 12:59:59', $timestamp]; // A leap year!

Chủ Đề