1

Hi I get an error when trying to get date interval using php strtotime function the code is:

 <?php
    $interval = time() - strtotime('1992/08/13');

    //expect to be 18
    // but the output is 1988
    print date('Y', $interval);
 ?>

any advice?

thanks

3
  • Your code works fine. Could you provide the actual error? Commented May 5, 2011 at 7:11
  • What are you trying to accomplish? Commented May 5, 2011 at 7:12
  • You're getting the number of seconds from your date to today. Commented May 5, 2011 at 7:12

4 Answers 4

4

If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP

There's an example of using it there that should allow you to do what you want to do.

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

3 Comments

+1 finally! someone else understood what the question is. DateInterval is the way to go. Check PHP version first though
Thanks. The inspiration for the blog post came from seeing loads of questions like this!
DateInterval is great; note however that it is available from PHP 5.3 only, and broken on Windows.
2

That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.

print $interval / (60*60*24*365.242199);

2 Comments

@meouw, Fixed for leap years :)
hehe, nice, over what time period will this be accurate?
0

Are you tring to get the years elapsed rather than the actual year?

If so:

$year = 31556926;

$interval = time() - strtotime('1992/08/13');
print round($interval / $year);

3 Comments

This is it. The reason why it prints out 1988 is because timestamps start january 1, 1970. 1970 + 18 = 1988
Pekka is on the money here. You can't treat a time interval like an absolute date.
I've no idea why this is downvoted. This is the only answer, everyone else is on the wrong track
0
$interval = time() - strtotime('1992/08/13');

These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.

If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out @James C's link and use his solutions. I just wanted to give some explanation.

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.