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 8567667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:56:03+00:00 2026-06-11T17:56:03+00:00

i wrote a simple method which from a date start it returns how many

  • 0

i wrote a simple method which from a date start it returns how many days hours and seconds were passed.

It returns for example 15000 days 3 hours 3 seconds ago … seems not good to read at user side.

How can i do better?

For example somenthing like facebook posts time?

can anyone take care on this code?

public function date_diff($start, $end="NOW")
    {
        $timeshift = false;
        $sdate = strtotime($start);
        $edate = strtotime($end);

        $time = $edate - $sdate;

        if($time >= 0 && $time <= 59) {
                // Seconds
              if($time == 1)
               {
                   $s = 'second';
               }
               else
               {
                   $s = 'seconds';
               }
                $timeshift = $time." ".$s;

        } elseif($time>=60 && $time<=3599) {
                // Minutes + Seconds
                $pmin = ($edate - $sdate) / 60;
                $premin = explode('.', $pmin);
                 if($premin[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

              //  $presec = $pmin-$premin[0];
                //$sec = $presec*60;

                $timeshift = $premin[0]." ".$m;//.round($sec,0); // sec ';

        } elseif($time>=3600 && $time<=86399) {
                // Hours + Minutes
                $phour = ($edate - $sdate) / 3600;
                $prehour = explode('.',$phour);

                $premin = $phour-$prehour[0];
                $min = explode('.',$premin*60);
                 if($prehour[0] > 1)
               {
                   $h = 'hours';
               }
               else
               {
                   $h = 'hour';
               }
                  if($min[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

           //     $presec = '0.'.$min[1];
            //    $sec = $presec*60;

                $timeshift = $prehour[0]." ".$h." ".$min[0]." ".$m;//.round($sec,0).' sec ';

        } elseif($time>=86400) {
                // Days + Hours + Minutes
                $pday = ($edate - $sdate) / 86400;
                $preday = explode('.',$pday);

                $phour = $pday-$preday[0];
                $prehour = explode('.',$phour*24);

                $premin = ($phour*24)-$prehour[0];
                $min = explode('.',$premin*60);

           //     $presec = '0.'.$min[1];
            //    $sec = $presec*60;


               if($preday[0] > 1)
               {
                   $d = 'days';
               }
               else
               {
                   $d = 'day';
               }
               if($prehour[0] > 1)
               {
                   $h = 'hours';
               }
               else
               {
                   $h = 'hour';
               }
               if($min[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

               $timeshift = $preday[0]." ".$d." ".$prehour[0]." ".$h." ".$min[0]." ".$m;

        }
        return $timeshift.' ago';
    }

thx and hope someone will enjoy this code

  • 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-11T17:56:04+00:00Added an answer on June 11, 2026 at 5:56 pm

    There was an issue on GitHub for the CodeIgniter 3 milestone, you can get many suggestions for your task there. There are a couple of variations available there: https://github.com/EllisLab/CodeIgniter/issues/309

    The bonus is that it’s going to be native to CodeIgniter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple Util method to convert a String in Java to util.Date
I am new to java.util.concurrent package and wrote a simple method which fetches some
I wrote this simple Java program which connects to internic server and returns the
I wrote simple class that on the start it just increase the value of
I wrote a simple plugin which sets some css code using wp_options. It all
I wrote a simple Tkinter based Python application that reads text from a serial
Hello I have a simple wcf service like this, with a test method which
I wrote a simple wxPython app which I package with py2app, turning it into
I am trying to implement a simple plist example from Beginning iPhone 3 Development
Problem is I have a very simple WCF REST service, which I wrote starting

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.