Adding days to $Date in PHP
Asked Answered
O

13

285

I have a date returned as part of a MySQL query in the form 2010-09-17.

I would like to set the variables $Date2 to $Date5 as follows:

$Date2 = $Date + 1

$Date3 = $Date + 2

etc., so that it returns 2010-09-18, 2010-09-19, etc.

I have tried

date('Y-m-d', strtotime($Date. ' + 1 day'))

but this gives me the date before $Date.

What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?

Oberstone answered 16/9, 2010 at 14:23 Comment(1)
possible duplicate of PHP Calculating future date by adding days to a variable dateMetric
B
568

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
Beauharnais answered 16/9, 2010 at 14:46 Comment(5)
It outputs correctly using just day as well... date("Y-m-d", strtotime('2010-09-17 + 1 day')) -> 2010-09-18, date("Y-m-d", strtotime('2010-09-17 + 2 day')) -> 2010-09-19Pacifically
Just tried it this way and it worked. I still have no idea why I got a value one day before my start date when I initially tried it.Oberstone
What if I had a variable for de number of days to add?Ranique
@Carlos echo date("Y-m-d", strtotime($Date.' + '.$variable.' days'));Frohman
Nope this method doesn't works at all. It is returning some date of 2004.Keeler
P
111

If you're using PHP 5.3, you can use a DateTime object and its add method:

$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');

Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).

Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):

$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
Pacifically answered 16/9, 2010 at 14:26 Comment(5)
Sorry Daniel, but the webserver runs php 5.2.6, so this won't workOberstone
DateTime::construct uses the same mechanism as strtotime to parse the date, so you can also do new DateTime("+1 day $date") which would not require 5.3Metric
This is the actual answer for php 5.5+Trough
@Steven ► meta.https://mcmap.net/q/109958/-web-page-designersMasaccio
@JoseManuelAbarcaRodríguez - Heh yea I tried that but apparently my vote is locked unless this answer is edited (according to SO)Conciliate
H
40

From PHP 5.2 on you can use modify with a DateTime object:

http://php.net/manual/en/datetime.modify.php

$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');

Be careful when adding months... (and to a lesser extent, years)

Hysterogenic answered 28/1, 2015 at 18:25 Comment(2)
accepted answer just gives answer for 15000+ days where this answer gives answer when you add 25000+ days too..Frohman
You can also query this on a DateTimeImmutable() and you get a modified copy without risk of altering the original.Fissure
P
23

Here is a small snippet to demonstrate the date modifications:

$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";

//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";

//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";

//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
Potation answered 25/2, 2013 at 6:7 Comment(0)
C
11

Here has an easy way to solve this.

<?php
   $date = "2015-11-17";
   echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>

Output will be:

2015-11-22

Solution has found from here - How to Add Days to Date in PHP

Conveyancer answered 22/11, 2015 at 18:46 Comment(0)
L
9

You can also use the following format

strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));

You can stack changes this way:

strtotime("+1 day", strtotime("+1 year", strtotime($date)));

Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.

Lobworm answered 14/12, 2012 at 11:9 Comment(0)
R
9

Using a variable for Number of days

$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date 
Ranique answered 18/6, 2014 at 8:25 Comment(1)
What is "$soma"? Some weird typo of "$newDate"?Halo
F
6

Here is the simplest solution to your query

$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days 
echo date_format($date,"Y-m-d"); //set date format of the result
Fingering answered 21/9, 2016 at 18:41 Comment(0)
A
2

This works. You can use it for days, months, seconds and reformat the date as you require

public function reformatDate($date, $difference_str, $return_format)
{
    return date($return_format, strtotime($date. ' ' . $difference_str));
}

Examples

echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
Arceliaarceneaux answered 8/10, 2021 at 7:42 Comment(0)
F
1

To add a certain number of days to a date, use the following function.

function add_days_to_date($date1,$number_of_days){
    /*
    //$date1 is a string representing a date such as '2021-04-17 14:34:05'
    //$date1 =date('Y-m-d H:i:s'); 
    // function date without a secrod argument returns the current datetime as a string in the specified format
    */
    $str =' + '. $number_of_days. ' days';
    $date2= date('Y-m-d H:i:s', strtotime($date1. $str));
    return $date2; //$date2 is a string
}//[end function]
Fiddlehead answered 28/4, 2021 at 19:49 Comment(0)
U
0

All have to use bellow code:

$nday = time() + ( 24 * 60 * 60);    
echo 'Now:       '. date('Y-m-d') ."\n";    
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Unshaped answered 14/12, 2012 at 10:51 Comment(1)
You shouldn't do it this way, eventually you will have problems with leap secondsOrmazd
H
0

Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.

$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);

After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.

$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);
Hageman answered 17/5, 2022 at 12:10 Comment(0)
B
-2

We can implement simply add or subtract days in the current date in php

date('Y-m-d' , strtotime('-90days'));
Bush answered 5/10, 2023 at 8:48 Comment(1)
OP says: "I have a date returned as part of a MySQL query in the form 2010-09-17." and they want to add days to that date. Please only answer the asked question.Tove

© 2022 - 2024 — McMap. All rights reserved.