1

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.

1

3 Answers 3

3

Because PHP < 5.3 doesn't support the DateInterval class, 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:

// Get the difference between the two dates, in seconds
$diff = $future_date->format('U') - $now->format('U');

// Calculate the days component
$d = floor($diff / 86400);
$diff %= 86400;

// Calculate the hours component
$h = floor($diff / 3600);
$diff %= 3600;

// Calculate the minutes component
$m = floor($diff / 60);

// Calculate the seconds component
$s = $diff % 60;

// $d, $h, $m and $s now contain the values you want, so you can just build a
// string from them
$str = "$d d, $h h, $m m, $s s";

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 $now from $future_date, not the other way around, or the result will be negative.

See it working

Sign up to request clarification or add additional context in comments.

Comments

1

What I have tried seems to work and is not so far away from what have You tried:

echo $now = date('Y-m-d H:i:s');
echo PHP_EOL;
echo $future = date('Y-m-d H:i:s', strtotime('today + 3 days'));
echo PHP_EOL;
echo date('d', (strtotime("{$future}") - strtotime("{$now}")));

Check http://codepad.org/JbaJ3EGH

You could transform date() into DateTime easily then. It is also neccessary to get only the days, hours, minutes or months from the subtracted dates as formatting the "3 days interval" into a date would lead to a "1970-01-01 00:00:00.000" date...

3 Comments

This would only work for intervals of less than 1 month, and is still subject to leap-second inaccuracies (although I think this is unavoidable in 5.2)
@DaveRandom Yes, therefore I am getting only the days difference. If the dates are more than a month it would be neccessary to get the months difference, subtract months and get the days difference. OK, I confess it is not the right approach and Your answer is much better!
I don't think there is a "right" approach to this. Your answer is not wrong and I'm not criticising, I'm just pointing out the flaw in the cunning plan. There are flaws in mine as well. Enjoy this delicious +1 I shall bestow upon you.
0

It may be useful to know that, in DaveRandom's very helpful answer, the values returned from floor() are floats (doubles), so

$h === 1 ? true : false;

will return false even if var_dump($h) prints 1.

If you need to check for values being integers (say you need to check for values being 1 or greater than one when creating strings with singular and plural versions of time units), cast them as integers when assigning them:

$h = (int) floor($diff / 3600);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.