Basically I want to get remaining time to expire. I did it easily in php 5.3.x like below
$now = new DateTime();
$future_date = new DateTime($enddate);
$interval = $future_date->diff($now,false); // ->diff will not work on 5.2.x
$interval->format("%d d, %h h, %i m");
How can I do the something in PHP : 5.2.x(5.2.17).
What I tried :
new DateTime(date('Y-m-d H:i:s', $now->format('U') - $future_date->format('U')));
Not providing expected result.
Because PHP < 5.3 doesn’t support the
DateIntervalclass,DateTime::diff()(which is the right way to do this) is unavailable. You will need to do this manually for it to work in 5.2.x.The math is actually quite simple:
However with larger intervals this will introduce inaccuracies, because it does not take leap seconds into account. This means that you could end up a few seconds out – but since your original format string does not contain a seconds component, I doubt this will matter too much for what you are doing.
Note also that you need to subtract
$nowfrom$future_date, not the other way around, or the result will be negative.See it working