从存储在另一个动态变量中的日期中减去动态值

从存储在另一个动态变量中的日期中减去动态值

问题描述:

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.

我需要从存储在动态变量'$ workdate'中的日期中减去天数。 p>

从日期中减去的天数也是动态生成的。 p>

  $ workdate = date('d / m /  y',strtotime($ wdate));  // $ wdate是从日期选择器中选择并从上一页传递的日期。
  code>  pre> 
 
 

我有一个名为'$ deduc'的变量,其值是动态生成的。 假设$ deduc = 2现在我需要做: p>

  $ workdate- $ deduc =?
 
 $ workdate = date('d / m / y',strtotime  ($ wdate)); 
 
而($ some_value = 0)
 {
 $ deduc = $ deduc + 1; 
 $ some_value  - ; 
 
} 
  code>  pre  > 
 
 

现在我需要减去$ deduc no。 来自$ workdate的日子。 请帮忙。 p> div>

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');

Demo

Take a look at the DateTime class and especially at the DateTime::sub() method.