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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:31:53+00:00 2026-06-09T16:31:53+00:00

There is this really nice function from the php.net documentation that enables you to

  • 0

There is this really nice function from the php.net documentation that enables you to format time in a Facebook-style manner (e.g., 2 minutes ago, 4 weeks ago, or 3 years ago).

However, I prefer the way Stackoverflow and Apple Mail does it which is generally as follows:

  1. The current day is listed in x seconds ago or x hours ago or time (e.g, 4:35pm).
  2. Yesterday is listed as “Yesterday”.
  3. All days after that are listed by M/D/Y.

Has anyone adapted this php.net script to do this or might share a different script that accomplishes the same goal?

<?php 

  function nicetime($date)
 {
    if(empty($date)) {
      return "No date provided";
    }

$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths         = array("60","60","24","7","4.35","12","10");

$now             = time();
$unix_date         = strtotime($date);

   // check validity of date
if(empty($unix_date)) {    
    return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {    
    $difference     = $now - $unix_date;
    $tense         = "ago";

} else {
    $difference     = $unix_date - $now;
    $tense         = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
    $difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
    $periods[$j].= "s";
}

  return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>
  • 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-09T16:31:55+00:00Added an answer on June 9, 2026 at 4:31 pm

    Ok, i answered my own question.

    The key is to track how many rounds of division the for loop goes through until the quotient of the current time minus the input time, $difference, divided by $jth value of the $lengths array item is less than the $j+1th value of this array.

    I track this by incrementing the variable $i (notice the if/elseif/else clause demonstrating each of the 3 points I mention above) in this modified version of nicetime():

         ///http://php.net/manual/en/function.time.php
      function nicetime($date)
     {
        if(empty($date)) {
          return "No date provided";
        }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }
    
    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
    
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    $i=0;
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $i++;
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    if($i<3){
        $day="$difference $periods[$j] {$tense}";
        return $day;
        //satisfies case #1 where time is listed as seconds, minutes, hours ago
    }
    
    elseif($i==3){
        $difference == 1 && $periods[$j]=='day' ? $day='yesterday':     
        $day="$difference $periods[$j] {$tense}";
        return $day;        
        //satisfies case #2 where time is listed as yesterday if not the current day
    
    }
    else{
        return $date;       
        // satisfies case #3 where date is listed as M/D/Y if greater than a week old
    }   
    
    }
    
    echo "case#1: ".nicetime('2012-08-13 23:12:16');
    echo "case#2: ".nicetime('2012-08-12 23:12:16');
    echo "case#3: ".nicetime('2012-07-07 23:12:16');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There maybe some simlar questions to this but I can't see anything that really
It's often the case that I can write a nice tight little VB.NET function,
It's a small thing, really: I have this function that converts dict objects to
Starting from this very nice article about doing facebook integration using the javascript API
I've got this really basic table structure: Posts PostXTags PostTags There is a 1-n
There might be a really simple solution to this question, but i just don't
I'm sure there's a really simple solution to this but I can't wrap my
I'm sure there is some really easy solution to this problem, at least it
I know there must be a really simple answer to this question, but I
If there is any possibility to make this code simpler, I'd really appreciate it!

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.