Add six months in php
I find working with DateTime much easier to use:
$datetime = new \DateTime();
$datetime->modify('+6 months');
echo $datetime->format('d');
or
$datetime = new \DateTime();
$datetime->add(new DateInterval('P6M'));
echo $datetime->format('d');
or in PHP version 5.4+
echo (new \DateTime())->add(new \DateInterval('P6M'))->format('d');
Adding three months to a date in PHP
Change it to this will give you the expected format:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
PHP datetime add 6 months and 1 year
You should look into php class DateInterval http://php.net/manual/en/class.dateinterval.php
Here's an example:
$converted = DateTime::createFromFormat("Y-m-d H:i:s", $originaldate);
$converted1Year = $converted->add(new DateInterval("P1Y"));//add one year //object reference is the same so adding a year altered original object and a reference to it is passed back, not copied
$converted2 = DateTime::createFromFormat("Y-m-d H:i:s", $originaldate);
$converted6months = $converted2->add(new DateInterval("P6M")); // add 6 months
And as suggested in the comments here's the DateTimeImmutable equivalent:
$converted = DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $originaldate);
$converted1Year = $converted->add(new DateInterval("P1Y"));
$converted6Months = $converted->add(new DateInterval("P6M"));
PHP DateTime::modify adding and subtracting months
Why it's not a bug:
The current behavior is correct. The following happens internally:
+1 month
increases the month number (originally 1) by one. This makes the date2010-02-31
.The second month (February) only has 28 days in 2010, so PHP auto-corrects this by just continuing to count days from February 1st. You then end up at March 3rd.
How to get what you want:
To get what you want is by: manually checking the next month. Then add the number of days next month has.
I hope you can yourself code this. I am just giving what-to-do.
PHP 5.3 way:
To obtain the correct behavior, you can use one of the PHP 5.3's new functionality that introduces the relative time stanza first day of
. This stanza can be used in combination with next month
, fifth month
or +8 months
to go to the first day of the specified month. Instead of +1 month
from what you're doing, you can use this code to get the first day of next month like this:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'first day of next month' );
echo $d->format( 'F' ), "\n";
?>
This script will correctly output February
. The following things happen when PHP processes this first day of next month
stanza:
next month
increases the month number (originally 1) by one. This makes the date 2010-02-31.first day of
sets the day number to1
, resulting in the date 2010-02-01.
PHP date within 6 months
Using DateTime, it becomes very trivial:
$now = new DateTime();
$input = DateTime::createFromFormat('m/Y', '4/2017');
$diff = $input->diff($now); // Returns DateInterval
// m is months
$lessThanSixMonths = $diff->y === 0 && $diff->m < 6; // true
See DateInterval
and DateTime
.
PHP: simplest way to get the date of the month 6 months prior on the first?
Hm, maybe something like this;
echo date("F 1, Y", strtotime("-6 months"));
EDIT;
if you would like to specify a custom date use;
echo date("F, 1 Y", strtotime("-6 months", strtotime("Feb 2, 2010")));
PHP add month to date on a date with day of month 31
I think this should work -
$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));
Related Topics
Check If Http Request Comes from My Android App
Run PHP Function Inside Jquery Click
How to Disable PHP Magic Quotes at Runtime
How to Get the Shortest Rather Than Longest Possible Regex Match with Preg_Match()
Adding Additional Persist Calls to Preupdate Call in Symfony 2.1
Format 32-Character String with Hyphens to Become Uuid
Read and Write to a File While Keeping Lock
Regex for Dropping Http:// and Www. from Urls
Properly Escaping with MySQLi | Query Over Prepared Statements
How to Parse HTML Table Using PHP
Send Email by Email Class in Codeigniter with Gmail
MySQL Query: Retrieve Current Date Query
How to Check If Allow_Url_Fopen Is Enabled or Not
How to Get Month from Date in MySQL
PHP Fatal Error: Call to Undefined Function Mssql_Query()
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method