Increase days to php current Date()
Asked Answered
L

11

60

How do I add a certain number of days to the current date in PHP?

I already got the current date with:

$today = date('y:m:d');

Just need to add x number of days to it

Lentiginous answered 10/11, 2008 at 6:59 Comment(0)
P
133

php supports c style date functions. You can add or substract date-periods with English-language style phrases via the strtotime function. examples...

$Today=date('y:m:d');

// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));

// subtract 3 days from date
$NewDate=Date('y:m:d', strtotime('-3 days'));

// PHP returns last sunday's date
$NewDate=Date('y:m:d', strtotime('Last Sunday'));

// One week from last sunday
$NewDate=Date('y:m:d', strtotime('+7 days Last Sunday'));

or

<select id="date_list" class="form-control" style="width:100%;">
<?php
$max_dates = 15;
$countDates = 0;
while ($countDates < $max_dates) {
    $NewDate=Date('F d, Y', strtotime("+".$countDates." days"));
    echo "<option>" . $NewDate . "</option>";
    $countDates += 1;
}
?>

Peterkin answered 10/11, 2008 at 7:11 Comment(2)
using the strtotime() function is very expensive and would be massive overkill if you just want to add a couple of days.Coagulase
Sounds like premature optimization. If it's not in a time-critical piece of code, and we have no reason to think it is, burning a few milliseconds is nothing compared to the cost of programmer time.Boyce
C
26

a day is 86400 seconds.

$tomorrow = date('y:m:d', time() + 86400);
Coagulase answered 10/11, 2008 at 7:3 Comment(3)
Although, for readability reasons you shoudn't use the Integer everywhere as-is. define a const somewhere or use 60 * 60 * 24 so its clear what the number represents.Shoulder
i use 86400 so often that it's as obvious to me now as it is that there's 60 seconds in a minute :). If I am doing a lot of time functions, then yes, I'll often declare it a constant.Coagulase
@Coagulase this is okay when you're the only one maintaining your code, but usually there are other people viewing it as well. If not now then maybe in future there will...Honeybunch
C
20

The simplest way to add x no. of days..

echo date('Y-m-d',strtotime('+1 day'));    //+1 day from today

OR from specified date...

echo date('Y-m-d',strtotime('+1 day', strtotime('2007-02-28')));
Cambogia answered 18/1, 2015 at 15:24 Comment(0)
U
8

With php 5.3

    $date = new DateTime();
    $interval = new DateInterval('P1D');
    echo $date->format('Y-m-d') , PHP_EOL;
    $date->add($interval);
    echo $date->format('Y-m-d'), PHP_EOL;
    $date->add($interval);
    echo $date->format('Y-m-d'), PHP_EOL;

will output

2012-12-24

2012-12-25

2012-12-26

Uncurl answered 25/12, 2012 at 3:28 Comment(0)
A
7

The date_add() function should do what you want. In addition, check out the docs (unofficial, but the official ones are a bit sparse) for the DateTime object, it's much nicer to work with than the procedural functions in PHP.

Asperges answered 10/11, 2008 at 7:3 Comment(0)
H
2

If you need this code in several places then I'd suggest that you add a short function to keep your code simpler and easier to test.

function add_days( $days, $from_date = null ) {
    if ( is_numeric( $from_date ) ) { 
        $new_date = $from_date; 
    } else { 
        $new_date = time();
    }

    // Timestamp is the number of seconds since an event in the past
    // To increate the value by one day we have to add 86400 seconds to the value
    // 86400 = 24h * 60m * 60s
    $new_date += $days * 86400;

    return $new_date;
}

Then you can use it anywhere like this:

$today       = add_days( 0 );
$tomorrow    = add_days( 1 );
$yesterday   = add_days( -1 );
$in_36_hours = add_days( 1.5 );

$first_reminder  = add_days( 10 );
$second_reminder = add_days( 5, $first_reminder );
$last_reminder   = add_days( 3, $second_reminder );
Honeybunch answered 24/4, 2015 at 14:50 Comment(0)
B
2
$NewDate=Date('Y-m-d', strtotime('+365 days'));

echo $NewDate; //2020-05-21

Bred answered 22/5, 2019 at 5:38 Comment(0)
B
0

Add 15 day to a select element (using "Alive to Die" suggestion)

<select id="date_list" class="form-control" style="width:100%;">
<?php
$max_dates = 15;
$countDates = 0;
while ($countDates < $max_dates) {
    $NewDate=Date('F d, Y', strtotime("+".$countDates." days"));
    echo "<option>" . $NewDate . "</option>";
    $countDates += 1;
}
?>

Bethezel answered 20/4, 2018 at 19:27 Comment(0)
P
0

$NewTime = mktime(date('G'), date('i'), date('s'), date('n'), date('j') + $DaysToAdd, date('Y'));

From mktime documentation:

mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.

The advantage of this method is that you can add or subtract any time interval (hours, minutes, seconds, days, months, or years) in an easy to read line of code.

Beware there is a tradeoff in performance, as this code is about 2.5x slower than strtotime("+1 day") due to all the calls to the date() function. Consider re-using those values if you are in a loop.

Perambulate answered 11/6, 2019 at 14:51 Comment(0)
D
0

You can also use Object Oriented Programming (OOP) instead of procedural programming:

$fiveDays = new DateInterval('P5D');
$today = new DateTime();
$fiveDaysAgo = $today->sub(fiveDays); // or ->add(fiveDays); to add 5 days

Or with just one line of code:

$fiveDaysAgo = (new DateTime())->sub(new DateInterval('P5D'));
Depress answered 29/11, 2020 at 9:23 Comment(0)
A
-2
<?php
$dt = new DateTime;
if(isset($_GET['year']) && isset($_GET['week'])) {
    $dt->setISODate($_GET['year'], $_GET['week']);
} else {
    $dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>

<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week-1).'&year='.$year; ?>">Pre Week</a> 
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week+1).'&year='.$year; ?>">Next Week</a>
<table width="100%" style="height: 75px; border: 1px solid #00A2FF;">
<tr>
<td style="display: table-cell;
    vertical-align: middle;
    cursor: pointer;
    width: 75px;
    height: 75px;
    border: 4px solid #00A2FF;
    border-radius: 50%;">Employee</td>
<?php
do {
    echo "<td>" . $dt->format('M') . "<br>" . $dt->format('d M Y') . "</td>\n";
    $dt->modify('+1 day');
} while ($week == $dt->format('W'));
?>
</tr>
</table>
Azotize answered 1/12, 2016 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.