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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:43:37+00:00 2026-06-15T19:43:37+00:00

I have a php function to do fuzzy time (aka time ago). This is

  • 0

I have a php function to do fuzzy time (aka time ago).

This is used when building a table from the server side, however now we are adding new items to the table through JavaScript, and we have the ability to select a date, therefore I need to duplicate the functionality in Javascript but have it accept the date in the format YYYY-MM-DD e.g. 2012-12-14.

I shall begin working on it, but I am terrible with dates in Javascript so have posted it here incase someone can do it faster.

The function is below:

/**
 * Convert date into a 'fuzzy' format
 *   -  15 minutes ago,  3 days ago, etc.
 * Pass a unix timestamp or a string to parse to a date.
 * @param string|number
 * @return string
 */
function fuzzyTime($date_from, $invalid_date = 'a long time ago') {

    $_time_formats = array(
        array(60, 'just now'),
        array(90, '1 minute'),
        array(3600, 'minutes', 60),
        array(5400, '1 hour'),
        array(86400, 'hours', 3600),
        array(129600, '1 day'),
        array(604800, 'days', 86400),
        array(907200, '1 week'),
        array(2628000, 'weeks', 604800),
        array(3942000, '1 month'),
        array(31536000, 'months', 2628000),
        array(47304000, '1 year'),
        array(3153600000, 'years', 31536000),
    );

    $now = time(); // current unix timestamp

    // if a number is passed assume it is a unix time stamp
    // if string is passed try and parse it to unix time stamp
    if (is_numeric($date_from)) {
        $dateFrom = $date_from;
    } elseif (is_string($date_from)) {
        $dateFrom = strtotime($date_from);
    }

    $difference = $now - $dateFrom; // difference between now and the passed time.
    $val = ''; // value to return

    if ($dateFrom <= 0) {
        $val = $invalid_date;
    } else {
        //loop through each format measurement in array
        foreach ($_time_formats as $format) {
            // if the difference from now and passed time is less than first option in format measurment
            if ($difference < $format[0]) {
                //if the format array item has no calculation value
                if (count($format) == 2) {
                    $val = $format[1] . ($format[0] === 60 ? '' : ' ago');
                    break;
                } else {
                    // divide difference by format item value to get number of units
                    $val = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
                    break;
                }
            }
        }
    }
    return $val;
}

This is what I have so far, but it returns 83 years ago

var date = '2012-11-14';

console.log(fuzzyTime(date));

function fuzzyTime($date_from, $invalid_date) {

    if($invalid_date === undefined){
        $invalid_date = 'a long time ago';
    }

    var $_time_formats = [
        [60, 'just now'],
        [90, '1 minute'],
        [3600, 'minutes', 60],
        [5400, '1 hour'],
        [86400, 'hours', 3600],
        [129600, '1 day'],
        [604800, 'days', 86400],
        [907200, '1 week'],
        [2628000, 'weeks', 604800],
        [3942000, '1 month'],
        [31536000, 'months', 2628000],
        [47304000, '1 year'],
        [3153600000, 'years', 31536000]
    ];

    var $now = new Date(); // current unix timestamp
    var $dateFrom = new Date($date_from);

    var $difference = Math.abs(new Date() - $dateFrom);

    var $val = ''; // value to return

    if ($dateFrom <= 0) {
        $val = $invalid_date;
    } else {
        for (var i = 0; i < $_time_formats.length; i++) {
            var $format = $_time_formats[i];

            if ($difference < $format[0]) {
                //if the format array item has no calculation value
                if ($format.length == 2) {
                    $val = $format[1] + ($format[0] === 60 ? '' : ' ago');
                    break;
                } else {
                    // divide difference by format item value to get number of units
                    $val = Math.ceil($difference / $format[2]) + ' ' + $format[1] + ' ago';
                    break;
                }
            }
        }

    }
    return $val;
}
  • 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-15T19:43:39+00:00Added an answer on June 15, 2026 at 7:43 pm

    Ah got it,

    var date = '2012-12-14 17:52:00';
    
    console.log(fuzzyTime(date));
    
    function fuzzyTime(date_from, invalid_date) {
    
        if (invalid_date === undefined) {
            invalid_date = 'a long time ago';
        }
    
        var time_formats = [
            [60, 'just now'],
            [90, '1 minute'],
            [3600, 'minutes', 60],
            [5400, '1 hour'],
            [86400, 'hours', 3600],
            [129600, '1 day'],
            [604800, 'days', 86400],
            [907200, '1 week'],
            [2628000, 'weeks', 604800],
            [3942000, '1 month'],
            [31536000, 'months', 2628000],
            [47304000, '1 year'],
            [3153600000, 'years', 31536000]
        ];
    
        var now = new Date().getTime(); // current unix timestamp
        var dateFrom = new Date(date_from.replace('-','/')).getTime();
    
        var difference = Math.abs(now - dateFrom)/1000;
    
        var val = ''; // value to return
    
        if (dateFrom <= 0) {
            val = invalid_date;
        } else {
            for (var i = 0; i < time_formats.length; i++) {
                var format = time_formats[i];
    
                if (difference < format[0]) {
                    //if the format array item has no calculation value
                    if (format.length == 2) {
                        val = format[1] + (format[0] === 60 ? '' : ' ago');
                        break;
                    } else {
                        // divide difference by format item value to get number of units
                        val = Math.ceil(difference / format[2]) + ' ' + format[1] + ' ago';
                        break;
                    }
                }
            }
    
        }
        return val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that extract dat of an invoice from DB. The
I have php function, that returns array to JavaScript like this: $data['first'] = 10;
I have a PHP function below that is used for a command line tool
i have php function that parses a xml url and gives me an array.this
I have this php function, which I want to convert to javascript function, but
For example, I have a php function: function DeleteItem($item_id) { db_query(DELETE FROM {items} WHERE
I have this php function that has to perform some processing on a given
I have this PHP function (using PHP 5.3) that I use to decrypt files,
I have a php function like this function printSelectedMembers($id) { $full_name = $this->getUserName($id); echo
I have a php function that fetches and returns tweets data from twitter as

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.