I need to subtract a number of days from a date which is stored in a dynamic variable '$workdate' .
The number of days to be subtracted from the date is also generating dynamically.
$workdate=date('d/m/y', strtotime($wdate)); //$wdate is a date selected from date picker and passed from previous page.
I have a variable named '$deduc' whose value is dynamically generating. suppose $deduc=2 now I need to do:
$workdate-$deduc=?
$workdate=date('d/m/y', strtotime($wdate));
while($some_value=0)
{
$deduc=$deduc+1;
$some_value--;
}
now I need to subtract $deduc no. of days from $workdate. Please do help.
Just create a DateTime
object with DateTime::createFromFormat()
and modify()
your date, e.g.
$date = DateTime::createFromFormat("d/m/y", $workdate);
echo $date->modify("- $deduc days")->format('Y-m-d');
Take a look at the DateTime class and especially at the DateTime::sub() method.