Adding Days to $Date in PHP

Adding days to $Date in PHP

All you have to do is use days instead of day like this:

<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>

And it outputs correctly:

2010-09-18
2010-09-19

add some days to current date in php

You can use strtotime() function to add days to current date. Please see the below :

 <?php
$date =date("Y-m-d");
$day = 5;
$newdate=date('Y-m-d', strtotime("+$day days"));
echo "today is:".$date;
echo "<br> and after 5 days is :".$newdate;
?>

Add 30 days to date

Please try this.

echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;

This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.

echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;

This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016

If the input date is going to be in mysql, you can perform this function on mysql directly, like this.

DATE_ADD(due_date, INTERVAL 1 MONTH);

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');

Adding Days to a Date with PHP

For the sake of completeness, here's how you do it with DateTime():

$datetime = new DateTime("2013-12-01");
$datetime->add(new DateInterval('P7D'));
echo $datetime->format('Y-m-d');

or

$datetime = new DateTime("2013-12-01");
$datetime->modify('+7 days');
echo $datetime->format('Y-m-d');

Add Days in format d/m/Y

Try this,

<?php
$start = '06/07/2017';
echo $start;
$start = str_replace("/","-",$start);
echo "<br>";
echo date("d/m/y", strtotime(date('d-m-Y', strtotime(' + 1 days', strtotime($start)))));

Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Source link.

Add days to a date (PHP)

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

Adding 1 day to day of week (Monday, Tuesday..)

You can simply do:

<?php
echo date('l', strtotime('Monday + 1 day'));

We just use the strtotime logic of adding a time period and wrap it in a date format of l - which is full text representation of the day.

l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday

refs:

https://secure.php.net/manual/en/function.strtotime.php

https://secure.php.net/manual/en/function.date.php

Adding days to specific day

For a very basic fix based on your code:

$day='2010-01-23';

// add 7 days to the date above
$NewDate = date('Y-m-d', strtotime($day . " +7 days"));
echo $NewDate;

If you are using PHP 5.3+, you can use the new DateTime libs which are very handy:

$day = '2010-01-23';

// add 7 days to the date above
$NewDate = new DateTime($day);
$NewDate->add(new DateInterval('P7D');
echo $NewDate->format('Y-m-d');

I've fully switched to using DateTime myself now as it's very powerful. You can also specify the timezone easily when instantiating, i.e. new DateTime($time, new DateTimeZone('UTC')). You can use the methods add() and sub() for changing the date with DateInterval objects. Here's documentation:

  • http://php.net/manual/en/class.datetime.php
  • http://php.net/manual/en/class.dateinterval.php

Adding days to date in php

From what it looks like you're trying to do, you don't even need $today (as it defaults to now if date is not supplied), so you could just do eg:

 $end_date = date("d/m/Y", strtotime("+ 5 days")); 
echo $end_date;

result would be

 30/04/2013

if you want to provide a date, you need the parameters the other way round, as per the manual:

strtotime ( string $time [, int $now = time() ] )


Related Topics



Leave a reply



Submit