Pete
Pete

Reputation: 713

php Date increment from array date

Having trouble getting a way to increment a string date by one day.

So it starts off with 30/01/2001 for example.

$linedata[8] = 30/01/2001;

Then I use date parse form format.

$thetime = date_parse_from_format("j/n/Y",$linedata[8]);

The return is in array. Is it possible to increment the date from this array or should i be parsing the date with a different function?

Upvotes: 0

Views: 137

Answers (3)

danmullen
danmullen

Reputation: 2510

This would work (providing $linedata[8] contains a date):

$thetime = date('j/n/Y', strtotime($linedata[8] . ' +1 day'));

Upvotes: 0

Matthias
Matthias

Reputation: 637

You can increment the date like this:

$thetime["day"]++;

or

$thetime["day"] = $thetime["day"] + 1;

then you can get the incremented date like this

$idate = $thetime["day"]."/".$thetime["month"]."/".$thetime["year"];

Upvotes: 0

John Conde
John Conde

Reputation: 219804

You don't want to use date_parse_from_format() as that just gives you a lot of date parts which requires you modifying the appropriate ones and then reassembling them. You also have issues with leap years and working with the last day of the month.

You want to use DateTime::createFromFormat() which gives you a DateTime object which is easy to work with and handles things like leap years and the number of days in a month:

$date = DateTime::createFromFormat("j/n/Y", '30/01/2001');
$date->modify('+1 day');
echo $date->format('Y-m-d');

Demo

Upvotes: 1

Related Questions