Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current date in PHP, and add 1 month to the current date?

Tags:

date

php

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?

Any ideas, suggestions. Cheers!

like image 562
user3714977 Avatar asked Jun 06 '14 12:06

user3714977


People also ask

How can I add current date in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How can I get 30 Day date in PHP?

php $next_due_date = date('05/06/2016', strtotime("+30 days")); echo $next_due_date; ?> But it is returning to "05/06/2016" only!

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!

How can get current date and date difference in PHP?

PHP date_diff() Function $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);


1 Answers

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");
like image 63
Ing. Michal Hudak Avatar answered Oct 02 '22 18:10

Ing. Michal Hudak