Converting PHP Fuzzy time to Javascript?

浪尽此生 提交于 2019-12-12 00:33:37

问题


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:


The moment.js library already has something to help you with doing this.

See http://momentjs.com/




回答2:


Theres a few librarys around to do this. heres one,

http://easydate.parshap.com/files/jquery.easydate-0.2.4.js

Its pretty old but I'd pull it apart and use the parts you need.




回答3:


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;
}


来源:https://stackoverflow.com/questions/13872685/converting-php-fuzzy-time-to-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!