$currentDT = new \DateTime();
$filterRange = new \DateInterval('PT30S');
$filterDate = $currentDT->sub($filterRange);
var_dump($currentDT);
var_dump($filterDate);
OUTPUT:
object(DateTime)[246]
public 'date' => string '2011-12-10 15:53:42' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/New_York' (length=16)
object(DateTime)[246]
public 'date' => string '2011-12-10 15:53:42' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/New_York' (length=16)
$currentDT and $filterDate are the same…even though they should be 30s different. Any idea why?
That is the expected behaviour, the subtraction acts on the original object which is then returned. This can be seen by the
246in thevar_dump()outputs, denoting that they’re one and the same object.If you wish to keep the original object untouched, you’ll need to
cloneit before doing the subtraction.