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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:59:28+00:00 2026-05-25T23:59:28+00:00

Say I have a date function that produces the output: 2011-10-03 PHP: $todayDt =

  • 0

Say I have a date function that produces

the output: 2011-10-03

PHP:

$todayDt = date('Y-m-d');

Anyway to get this date to instead show 2 days 1 hour 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-05-25T23:59:28+00:00Added an answer on May 25, 2026 at 11:59 pm

    This function might be of some use. You might want to refine the check for months a bit, but this is just a quick example:

    function RelativeTime($iTimestamp, $iLevel = 2)
    {
        !ctype_digit($iTimestamp)
            && $iTimestamp = strtotime($iTimestamp);
    
        $iSecondsInADay = 86400;
        $aDisplay = array();
    
        // Start at the largest denominator
        $iDiff = time() - $iTimestamp;
        $aPeriods = array(
            array('Period'  => $iSecondsInADay * 356,   'Label'   => 'year'),
            array('Period'  => $iSecondsInADay * 31,    'Label'   => 'month'),
            array('Period'  => $iSecondsInADay,         'Label'   => 'day'),
            array('Period'  => 3600,                    'Label'   => 'hour'),
            array('Period'  => 60,                      'Label'   => 'minute'),
            array('Period'  => 1,                       'Label'   => 'second'),
        );
    
        foreach ($aPeriods as $aPeriod)
        {
            $iCount = floor($iDiff / $aPeriod['Period']);
            if ($iCount > 0)
            {
                $aDisplay[] = $iCount . ' ' . $aPeriod['Label'] . ($iCount > 1 ? 's' : '');
                $iDiff -= $iCount * $aPeriod['Period'];
            }
        }
    
        $iRange = count($aDisplay) > $iLevel
                    ? $iLevel
                    : count($aDisplay);
        return implode(' ', array_slice($aDisplay, 0, $iRange)) . ' ago';
    }
    

    And some examples of usage:

    echo RelativeTime(time() - 102, 1);
    // Will output: 1 minute ago
    
    echo RelativeTime(time() - 2002);
    // Will output: 33 minutes 22 seconds ago
    
    echo RelativeTime(time() - 100002002, 6);
    // Will output: 3 years 2 months 27 days 10 hours 20 minutes 2 seconds ago
    
    echo RelativeTime('2011-09-05');
    // Will output: 30 days 22 hours ago
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a function that returns a date: Date myFunc(paramA, paramB){ //conditionally
1st i have to say that i am not php professional and this is
Let's say I have a date that I can represent in a culture-invariant format
Let's say I have an object: public class CustomObj { DateTime Date { get;
So let's say I have a view that access a date: <%= Html.TextBoxFor(model =>
Let’s say I have a function that accepts a user name and password, it
Let's say I have this form and script: <SCRIPT LANGUAGE=JavaScript> function handleSubmit() { return
Say I have some code like this function Chart(start, end, controller, method, chart) {
Lets say I have a named range of dates that are holidays. This range
Say I have a Date, or a Phone number, or some Class which I

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.