How do I show the difference between two dates and times in php?


How do I show the difference between two timestamps (which both show the date and time) in php? I have a formula that shows the difference between 2 timestamps, but the format that it shows the difference in is in days/ hours/ minutes/ seconds. I only want one unit to appear. For instance:
If it has been less than a minute between those two dates, I want to return the number of seconds between those dates.

If it’s been less than an hour between the dates, return the number of mintues between the dates.

If less than a day, return the number of hours.

I had more, but if I get those or at least a few of those, I can figure out the rest. Thanks.
I am still accepting solutions. Answer 1 is not acceptable. Please don’t post links unless it is a link to an actual solution.

Related posts:

  1. What is the code to show general section in interests on myspace?
  2. What is the difference between computer programming and network administration?
  3. What is the difference between a programmer and a computer scientist?

2 Responses to “How do I show the difference between two dates and times in php?”

  • my_screen_name:

    there is no out of the box solution as your question is very specific, here is an idea.

    use strtotime or mktime to convert your date time to a unix timestamp. once you have two unix time stamps you can get the difference in seconds.

    so

    $diff < 60 // difference is less than a minute

    $diff < (60 * 60) // diff less than one hour

    $diff < (60 * 60 * 24) // diff less than a day

    after that you need mathematics to convert your seconds of difference in to readable hour minute format.

  • Roland J:

    Look up date functions on http://www.php.net

    RJ

Leave a Reply