[Tutorial] How to: Unix timestamps
#14

Here are the 2 functions I wrote in PHP:
Code:
function diff($int1, $int2) {
        if ($int1 > $int2) {
                return $int1 - $int2;
        }
        return $int2 - $int1;
}

function timec($timestamp, $compare = null) {
        if ($compare === null) {
                $compare = time();
        }
        $diff = diff($timestamp, $compare);
        if ($diff < 60) {
                return '< 1 minute';
        } else if ($diff < 3600) {
                $num = floor($diff / 60).' minute';
        } else if ($diff < 86400) {
                $num = floor($diff / 60 / 60).' hour';
        } else if ($diff < 2592000) {
                $num = floor($diff / 60 / 60 / 24).' day';
        } else if ($diff < 31536000) { // 31536000 = 1 year
                $num = floor($diff / 60 / 60 / 24 / 30).' month';
        } else {
                return 'A very very long time';
        }
        if ($num > 1) {
                return $num.'s';
        }
        return $num;
}
I have made an attempt at converting it to pawn (not tested but should work!):

pawn Code:
stock timec(timestamp, compare = -1) {
    if (compare == -1) {
        compare = gettime();
    }
    new
        n,
        // on the following line, I have removed the need for the diff() function.
        // if you want to use the diff() function in pawn, replace the following with:
        // Float:d = diff(timestamp, compare),
        Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
        returnstr[32];
    if (d < 60) {
        format(returnstr, sizeof(returnstr), '< 1 minute');
        return returnstr;
    } else if (d < 3600) { // 3600 = 1 hour
        n = floatround(floatdiv(d, 60.0), floatround_floor);
        format(returnstr, sizeof(returnstr), 'minute');
    } else if (d < 86400) { // 86400 = 1 day
        n = floatround(floatdiv(d, 3600.0), floatround_floor);
        format(returnstr, sizeof(returnstr), 'hour');
    } else if (d < 2592000) { // 2592000 = 1 month
        n = floatround(floatdiv(d, 86400.0), floatround_floor);
        format(returnstr, sizeof(returnstr), 'day');
    } else if (d < 31536000) { // 31536000 = 1 year
        n = floatround(floatdiv(d, 2592000.0), floatround_floor);
        format(returnstr, sizeof(returnstr), 'month');
    } else {
        n = floatround(floatdiv(d, 31536000.0), floatround_floor);
        format(returnstr, sizeof(returnstr), 'year');
    }
    if (n == 1) {
        format(returnstr, sizeof(returnstr), '1 %s', returnstr);
    } else {
        format(returnstr, sizeof(returnstr), '%d %ss', n, returnstr);
    }
    return returnstr;
}
Reply


Messages In This Thread
How to: Unix timestamps - by __ - 13.05.2011, 15:22
Re: How to: Unix timestamps - by CrunkBankS - 13.05.2011, 16:58
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:11
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:39
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:43
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:58
Re: How to: Unix timestamps - by Zimon95 - 13.05.2011, 18:34
Re: How to: Unix timestamps - by __ - 14.05.2011, 10:10
Re: How to: Unix timestamps - by Haydz - 16.05.2011, 10:52
Re: How to: Unix timestamps - by linuxthefish - 16.05.2011, 11:40
Re: How to: Unix timestamps - by Swiftz - 16.05.2011, 15:47
Re: How to: Unix timestamps - by Zimon95 - 16.05.2011, 17:57
Re: How to: Unix timestamps - by __ - 17.05.2011, 08:39
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 00:23
Re: How to: Unix timestamps - by __ - 19.05.2011, 10:04
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 14:30
Re: How to: Unix timestamps - by __ - 19.05.2011, 14:42
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 16:38
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 22:00
Re: How to: Unix timestamps - by Scenario - 14.06.2011, 19:30
Re: How to: Unix timestamps - by Anzipane! - 14.06.2011, 20:39
Re: How to: Unix timestamps - by Blacklite - 14.06.2011, 21:14
Re: How to: Unix timestamps - by jot16 - 14.06.2011, 21:17
Re: How to: Unix timestamps - by Jack_Rocker - 14.06.2011, 21:20
Re: How to: Unix timestamps - by Iphone1234g - 15.06.2011, 09:41
Re: How to: Unix timestamps - by __ - 17.06.2011, 23:08
Re: How to: Unix timestamps - by Steamator - 20.06.2011, 14:45
Re: How to: Unix timestamps - by ||123|| - 14.07.2011, 07:26
Re: How to: Unix timestamps - by cessil - 14.07.2011, 12:50
Re: How to: Unix timestamps - by __ - 15.07.2011, 05:39
Re: How to: Unix timestamps - by Jantjuh - 16.07.2011, 19:49
Re: How to: Unix timestamps - by BrUn3S - 18.10.2011, 18:59
Re: How to: Unix timestamps - by Scenario - 17.12.2011, 00:28
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:02
Re: How to: Unix timestamps - by Scenario - 07.01.2012, 20:07
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:14
Re: How to: Unix timestamps - by Calgon - 23.03.2012, 10:42
Re: How to: Unix timestamps - by __ - 15.06.2012, 07:19
Re: How to: Unix timestamps - by Tanush123 - 22.05.2013, 21:03
Re: How to: Unix timestamps - by trukker1998 - 19.02.2014, 11:35
Re: How to: Unix timestamps - by Scenario - 19.02.2014, 11:53
Re: How to: Unix timestamps - by AroseKhanNiazi - 01.03.2014, 11:38
Re: How to: Unix timestamps - by _Application_ - 20.01.2015, 11:20
Re: How to: Unix timestamps - by PowerPC603 - 20.01.2015, 13:37
Re: How to: Unix timestamps - by AdamCooper - 07.10.2018, 00:45

Forum Jump:


Users browsing this thread: 5 Guest(s)