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

  • SEARCH
  • Home
  • 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 480123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:51:40+00:00 2026-05-13T00:51:40+00:00

You know when it’s late in the night and your brain is fried? I’m

  • 0

You know when it’s late in the night and your brain is fried? I’m having one of those nights right now, and my function so far is not working as it should, so please take a look at it:
(I should note that I’m using the PHP 5.2.9, and the function / method DateTime:Diff() is not available until PHP 5.3.0.

<?php
    function time_diff($ts1, $ts2) {
        # Find The Bigger Number
        if ($ts1 == $ts2) {
            return '0 Seconds';
        } else if ($ts1 > $ts2) {
            $large = $ts1;
            $small = $ts2;
        } else {
            $small = $ts1;
            $large = $ts2;
        }
        # Get the Diffrence
        $diff = $large - $small;
        # Setup The Scope of Time
        $s = 1;         $ss = 0;
        $m = $s * 60;   $ms = 0;
        $h = $m * 60;   $hs = 0;
        $d = $h * 24;   $ds = 0;
        $n = $d * 31;   $ns = 0;
        $y = $n * 365;  $ys = 0;
        # Find the Scope
        while (($diff - $y) > 0) { $ys++; $diff -= $y; }
        while (($diff - $n) > 0) { $ms++; $diff -= $n; }
        while (($diff - $d) > 0) { $ds++; $diff -= $d; }
        while (($diff - $h) > 0) { $hs++; $diff -= $h; }
        while (($diff - $m) > 0) { $ms++; $diff -= $m; }
        while (($diff - $s) > 0) { $ss++; $diff -= $s; }
        # Print the Results
        return "$ys Years, $ns Months, $ds Days, $hs Hours, $ms Minutes & $ss Seconds.";
    }
    // Test the Function:
    ediff(strtotime('December 16, 1988'), time());
    # Output Should be:
    # 20 Years, 11 Months, 8 Days, X Hours, Y Minutes & Z Seconds.
?>
  • 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-05-13T00:51:40+00:00Added an answer on May 13, 2026 at 12:51 am

    How about this:

    function time_diff($t1, $t2)
    {
       $totalSeconds = abs($t1-$t2);
       $date = getdate($totalSeconds); 
       $firstYear = getdate(0);
       $years = $date['year']-$firstYear['year'];
       $months = $date['mon'];
       $days = $date['mday'];
       $hours = $date['hour'];
       $minutes = $date['minutes'];
       $seconds = $date['seconds'];
    
       return "$years Years, $months Months, $days Days, $hours Hours, $minutes Minutes & $seconds Seconds.";
    }
    

    This uses the difference of the given times as a date. Then you can let the “getdate” do all the work for you. The only challenge is the number years – which is simply the getdate year (of the difference) minus the Unix epoch year (1970).

    If you don’t like using an actual month, you could also divide the “year” day by the number of days in 12 equal months

    $months = $date['yday'] / (365/12);
    

    Similarly days could be figured out the remaining days with modulus

    $days = $date['yday'] % (365/12);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a stupid question, but I've looked for 45 mins now
I know of cufon and one other similar. But what is the best way
I know that there's something fishy about the malloc part, but I'm having trouble
Anyone know how to get the id of multiple placemarks each one created with
I know how to slice array with numpy, for example v[1, :, :] now
I know we can do correlated subqueries and join. But which one is faster?
I know about this question , but I have the source and I'm having
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
I know that there has been one question about this but it is not
I know some basic about iOS programming, now i want to connect my app

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.