Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:41:01+00:00 2026-06-12T01:41:01+00:00

I want to do what StackOverflow is doing which is saying exactly how long

  • 0

I want to do what StackOverflow is doing which is saying exactly how long it’s been since the last post. There is a catch though, SO displays certain information based on how long the ago the last post was – for example, if the post was less than a day ago, they post how many hours ago the last post was; if the post was less than an hour ago they post how many minutes ago it was, etc.

I’m working with a MYSQL DateTime field in the following format:

2012-09-19 13:28:45

I want to compare the above to the time NOW and so I converted that time using PHP’s strtotime function and tried comparing the two dates through a function I put together (below). Granted, this is probably the WORST possible way of doing this but after reading about PHP’s Date and DateTime functions I’m starting to become very, very confused.

function get_date_format($strToTimeString){

        $minute = 60;
        $hour = $minutes * 60;
        $day = $hour * 24;
        $week = $day * 7;
        $month = $week * 4;
        $year = $month * 12;

        $timeNow = strtotime("now");

        $timeDiff = $timeNow - $strToTimeString;

        if($timeDiff > $minute){
            if($timeDiff > $hour){
                if($timeDiff > $day){
                    if($timeDiff > $week){
                        if($timeDiff > $month){
                            if($timeDiff > $year){
                                // Years ago
                            }
                            else{
                                // Months ago
                            }
                        }
                        else{
                            // Weeks ago
                        }
                    }
                    else{
                        // Days ago
                    }
                }
                else
                {
                    // Hours ago
                }
            }
            else{
                // Minutes ago
            }

        }
        else{
            // Seconds ago
        }


    }

Is there a better way to do this? As I mentioned above, I had no luck when trying to use DateTime->diff

I really appreciate any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:41:02+00:00Added an answer on June 12, 2026 at 1:41 am

    Use DateTime and DateTime:diff then check each value:

    function returnInterval($date){
        $datetime1 = new DateTime($date);
        $datetime2 = new DateTime();
        $diff = $datetime1->diff($datetime2);
        $string = '';
        $pass = '';
    
        if($diff->y){
            $string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";
            $pass = ', ';
        }
        if($diff->m){
            $string .= $pass;
            $string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";
            $pass = ', ';
        }
        if($diff->d){
            $string .= $pass;
            $string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";
            $pass = ', ';
        }
        if($diff->h){
            $string .= $pass;
            $string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";
            $pass = ', ';
        }
        if($diff->i){
            $string .= $pass;
            $string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";
            $pass = ', ';
        }
        if($diff->s){
            $string .= $pass;
            $string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";
        }
        $pos = strrpos($string, ',');
        $string = substr_replace($string, ' and ', $pos, 2);
        return $string;
    }
    
    echo returnInterval('2012-09-19 13:28:45');
    // 8 days, 13 hours, 47 minutes and 44 seconds
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing Windows programming in .Net since last two years. Now I
I found this post on stackoverflow which was exactly what I wanted to integrate
This is my first post in stackoverflow. I want to improve its google pagerank
There are a few questions on Stackoverflow which ask about aligning elements horizontally using
i want to make a text box like stackoverflow have for the tag at
I want to say hello to the stackoverflow community. I've just started using knockout
I want to make a website on the lines of stackoverflow. I need some
So I want to do look a like stackoverflow with the message at the
I want to get the string length when a key is pressed like StackOverflow
This is the URL I want to share: http://mydomain.com/#url=http://stackoverflow.com Inside my site, I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.