Increase current date by 5 days

$date = date('Ym-d',current_time('timestamp', 0)); 

How do I change $date to $date + 5 days ?

PHP version is 5.2.

This code does not work:

 $date_cur = date('Ym-d',current_time('timestamp', 0)); echo $date_cur . ' <br>'; $date_cur_plus = date($date_cur, strtotime('+5 days')); echo $date_cur_plus; 

Gives me:

 2011-11-29 2011-11-29 
+4
source share
10 answers

You can use mktime() using a timestamp.

Sort of:

 $date = date('Ym-d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y'))); 

Using strtotime() is faster, but my method still works and is flexible when you need to make a lot of changes. In addition, strtotime() cannot handle ambiguous dates.

Edit

If you need to add 5 days to an existing date string in YYYY-MM-DD format, you can split it into an array and use those parts with mktime() .

 $parts = explode('-', $date); $datePlusFive = date( 'Ym-d', mktime(0, 0, 0, $parts[1], $parts[2] + 5, $parts[0]) // ^ Month ^ Day + 5 ^ Year ); 
+16
source
 $date = date('Ym-d', strtotime('+5 days')); 
+20
source

Object Oriented Style:

 <?php $date = new DateTime('now'); $date->add(new DateInterval('P5D')); echo $date->format('Ym-d') . "\n"; ?> 

Procedural style:

 <?php $date = date_create('2016-01-01'); date_add($date, date_interval_create_from_date_string('5 days')); echo date_format($date, 'Ym-d'); ?> 
+3
source

Use strtotime :

 $date = date('Ym-d', strtotime('+5 days')); 
+1
source
 $dateplus5 = date('Ym-d', strtotime('+5 days')); 
+1
source

you can use

 strtotime("+5 days") 

to get the current date plus 5 days or

 $targetDate = date($date, strtotime('+5 days')); 
+1
source

strtotime() very nice. It allows you to do the following:

 $startDate = 'now'; // or choose a certain date you want/have $startDate = '2013-02-14'; $intervals = array( '', '+ 5 days', '+ 31 days', '+ 3 months', '+ 2 years + 2 days' ); foreach($intervals as $interval) { $combinedDate = $startDate . ' ' . $interval; var_dump($combinedDate . ' => ' date('Ym-d', strtotime($combinedDate))); } 

with the result:

now => 1360334498 = 2013-02-08

now + 5 days => 1360766498 = 2013-02-13

now + 31 days => 1363012898 = 2013-03-11

now + 3 months => 1368020498 = 2013-05-08

now + 2 years + 2 days => 1423579298 = 2015-02-10

or

2013-02-14 => 1360792800 = 2013-02-14

2013-02-14 + 5 days => 1361224800 = 2013-02-19

2013-02-14 + 31 days => 1363471200 = 2013-03-17

2013-02-14 + 3 months => 1368478800 = 2013-05-14

2013-02-14 + 2 years + 2 days => 1424037600 = 2015-02-16

+1
source

I used this:

 $date = strtotime("+1 day", strtotime("2007-02-28")); echo date("Ymd", $date); 

Now it works.

0
source

Shouldn't have been like that?

 $date_cur = date('Ym-d', current_time('timestamp', 0)); echo $date_cur . ' <br>'; $date_cur_plus = date('Ym-d', strtotime('+5 days', current_time('timestamp', 0) ) ); echo $date_cur_plus; 
0
source

if the date already exists, you can use this code:

 $tartDate = date("m/d/Y", strtotime("+1 Day", strtotime($Date))); 
0
source

Source: https://habr.com/ru/post/1383590/


All Articles