PHP - Add one week to a user defined date
You can try this
$start_date = "2015/03/02";
$date = strtotime($start_date);
$date = strtotime("+7 day", $date);
echo date('Y/m/d', $date);
PHP subtract 1 month from date formatted with date ('m-Y')
<?php
echo $newdate = date("m-Y", strtotime("-1 months"));
output
07-2016
php - add + 7 days to date format mm dd, YYYY
$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
subtract 2 days from a date which is in m-d-Y H:i:s format in PHP
$currentdate= date('m-d-Y H:i:s');
echo $currentdate; //gives 06-22-2019 14:58:55
$date = date('m-d-Y H:i:s ', strtotime('-2 days', strtotime(date('Y-m-d H:i:s'))));
echo $date; //gives 06-20-2019 14:58:55
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.
Date minus 1 year?
You can use strtotime
:
$date = strtotime('2010-01-01 -1 year');
The strtotime
function returns a unix timestamp, to get a formatted string you can use date
:
echo date('Y-m-d', $date); // echoes '2009-01-01'
php get oldest record and add 1 week
You can use the Datetime object to add a week easily
http://php.net/manual/en/book.datetime.php
$date = new DateTime('2013-03-20 09:42:41');
$date->modify('+1 week');
format date and add or subtract number of days in date coming from mysql using php
As long as $result['startDate'] is a date type column, and you're on a version of php greater than 5.0.2, you're looking for:
$startDate = $result['startDate'];
$date=date('Y-m-d',strtotime($startDate. ' +1 day'));
echo $date;
Adding one day to a date
<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date;
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>
For PHP 5.2.0+, you may also do as follows:
$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s');
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');
Related Topics
Detecting Whether a User Is Behind a Proxy
What Security Problems Could Come from Exposing PHPinfo() to End Users
Replace Deprecated Preg_Replace /E with Preg_Replace_Callback
How to Take an Array, Divide It by Two and Create Two Lists
How Is an Array in a PHP Foreach Loop Read
MySQL Query In() Clause Slow on Indexed Column
Single Session Login in Laravel
Creating an Image Without Storing It as a Local File
PHP Soap Client for Uk Mail Webservice API
Fatal Error: Call to a Member Function Query() on Null
Parsing JavaScript (Not JSON) in PHP
How to Make Apache Serve Index.PHP Instead of Index.Html
PHP Soap Client That Understands Multi-Part Messages