0

Quick embrassing question.

I have been looking for a PHP function that would calculate the difference between two timestamps and output the result based on given parameters such as

the diff in years only, diff in months only, diff in days only, etc etc

The function I made has been quite buggy and I haven't found a good one on the Net.

Please assist.

Thanks

Stanley Ngumo
  • 4,089
  • 8
  • 44
  • 64
  • Maybe you show us your code so we understand your question better? – powtac Mar 02 '11 at 11:27
  • I guess from your question that you want decimal numbers like 1.278 years = 15.336 months = 466.47 days? For that you would first have to answer the questions "How many days are in a year?" and "How many days are in a month?"... – Martin Hennings Mar 02 '11 at 11:32
  • possible duplicate of [Difference between dates](http://stackoverflow.com/questions/3865079/difference-between-dates) – Gordon Mar 02 '11 at 11:32
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – outis Mar 02 '11 at 12:20

1 Answers1

1

Please take a look at DateTime::diff()

DateTime::diff — Returns the difference between two DateTime objects

You can format the output to anything you want it to be

(Provided an extra answer despite of the duplicates because they use strtotime & math and that doesn't always work out well or is a nice way to do it. Using a core function of php seems nicer to me)

edorian
  • 38,542
  • 15
  • 125
  • 143