Add one month to gmdate using current format [duplicate] - php

This question already has answers here:
Add a 1-hour expiration to the current time/date
(5 answers)
Closed 7 years ago.
Hello I have the following php code:
$date = gmdate("Y-m-d\TH:i:s\Z"); /*Format: '2015-05-24T00:00:00Z'*/
How would I simply add 1 month to the above date while keeping the same format?
I tried a few things that lead to nowhere, I am sure there should be a way of doing it that I can't seem to find.
Any help is much appreciated!
Thanks,
Al

try below:
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime("+1 month"));

Try with -
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime('+ 1 MONTH'));
echo $date;
It will add one month to the date.

Related

PHP strotime - String to Date - wrong date result [duplicate]

This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 5 years ago.
When I try to convert very high dates, such as 2045-01-01, I get another date:
date("Ymd", strtotime("2045-02-15"));
I obtain a wrong date
19700101
but when
date("Ymd", strtotime("2017-02-15"));
I have the good date
20170215
I don't understand why? Someone just explain to me what's going on?
that's a unixtimestamp problem (=> https://de.wikipedia.org/wiki/Unixzeit#/media/File:Year_2038_problem.gif), better:
date_parse("2006-12-12 10:00:00");
date_parse_from_format ( 'Ymd' , "2017-02-15" );
or
$date = DateTime::createFromFormat('Ymd', "2017-02-15");
echo $date->format('Y-m-d');

Add days to a date (PHP) [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 9 years ago.
I am saving dates like so: 2013-11-06 using date function
date("Y-m-d")
Would like to add three days to the date. Would I need to use strtotime() for this?
Add days to a date (PHP)
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
you may find here more Date time
Update
How to find time difference between two dates using PHP:
Answer
How to calculate the difference between two dates using PHP?: Answer
Here you go
echo date('Y-m-d', strtotime("+3 days"));
Here, really easy:
$date=date("Y-m-d");
$date_plus_3_days=date("Y-m-d", strtotime("+3 days"));
strtotime can do lots of other related things: PHP.net documentation.
You can also use this.
$date = new DateTime(date("Y-m-d"));
$date->add(new DateInterval('P3D'));
echo $date->format('Y-m-d');

Format date as the first/last day of the month [duplicate]

This question already has answers here:
In PHP, is there an easy way to get the first and last date of a month?
(12 answers)
Closed 9 years ago.
I have date strings in "Y-m" format. Day is irrelevant here. Now I need to create another date from these date strings wheere day should be either 1st or the last day of the month. I have tried this code:
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-d');
The problem here is that it creates dates with the current day of the months. How can I force it to do it for the 1st or last day?
Thanks
first day:
$dt = $curDate->format('Y-m-01');
last day:
$dt = $curDate->format('Y-m-t');
t: Number of days in the given month
http://php.net/manual/en/function.date.php
You can specify hard code value
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-1'); //if you want first day
$dt = $curDate->format('Y-m-t'); //if you want last day

Get the following day after a month [duplicate]

This question already has answers here:
PHP strtotime +1 month adding an extra month [duplicate]
(7 answers)
How can I get a date after 15 days/1 month in PHP?
(6 answers)
Closed 9 years ago.
Just a noob question,
For example I have date 2013-08-01 // 1 August 2013
How do I get same day next month. ex. 2013-09-01 // 1 Sept 2013
Thanks
I'm not a PHP dev, but it seems to me you want DateTime::add
$date->add(new DateInterval('P1M'));
As noted in the documentation though, you need to be careful near the end of a month - adding 1 month to January 31st 2001 ends up with March 3rd, for example. (Not the behaviour I'd choose, but that's a different matter.) If you're always dealing with the 1st of the month, you should be okay.
With using strtotime
echo date("Y-m-d",strtotime('next month',strtotime('2013-08-01')));
You can use the date_add function (procedural) or Datetime::add (OO). Here is an example of each:
$date = new DateTime('2000-01-01');
$date->add(new DateInterval::createFromDateString('1 month'));
or
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 month'));
Try this:
$dateTest = date('Y-m-d', strtotime("$dateTest + 1 month"));

Change date 2013-08-09 => 09-Aug-2013 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I want to change format of date by 2013-08-09 to 09-Aug-2013, I tried this:
echo $date=date("Y-m-d");
//output 2013-08-09
echo date("d-M-Y",mktime(0-0-0,$date));
but this code showing 10-Aug-2013
I dont know why this showing date 10 instead of 09.
Answer would be highly appreciated , thanks in advance
following is easier:
echo date("d-M-Y",strtotime($date));
at mktime you have to subtract one day
You aren't passing in the parameters to mktime properly. mktime requires 6 integer parameters where as you have 1 integer and 1 string here.
Try using a DateTime object instead:
$date = new DateTime();
echo $date->format('d-M-Y');

Categories