Be careful when using this function, I may have happened upon a bug in PHP7.
My code is as follows
//get date from post or else fill with today's date
if (isset($_POST["from"]))
{
$from = date_create($_POST["from"]);
}else{
$from = date_create(date("Y-m-d"));
}
//get date from post if there isn't one just take the same date as what is in the $from variable and add one day to it
if (isset($_POST["to"]))
{
$to = date_create($_POST["to"]);
}else{
$to = $from;
date_modify($to, '+1 day');
}
echo(date_format($from, 'Y-m-d') . " " . date_format($to, 'Y-m-d'));
The resultant output is
$from = 2015-12-11
$to = 2015-12-11
In actuality the result should be
$from = 2015-12-10
$to = 2015-12-11
For some reason the code above modifies the $from variable in the line date_modify($to, '+1 day'); even though it shouldn't as the $from variable isn't being modified.
to fix this i needed to change the code to
//get date from post or else fill with today's date
if (isset($_POST["from"]))
{
$from = date_create($_POST["from"]);
}else{
$from = date_create(date("Y-m-d"));
}
//get date from post if there isn't one just take the same date as what is in the $from variable and add one day to it
if (isset($_POST["to"]))
{
$to = date_create($_POST["to"]);
}else{
$to = date_create(date("Y-m-d"));
date_modify($to, '+1 day');
}
echo(date_format($from, 'Y-m-d') . " " . date_format($to, 'Y-m-d'));
This isn't strictly the code I wanted. Possible bug?