We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

human-time.php 3.69 KB
Newer Older
fauno's avatar
fauno committed
1
<?php
jurgbohn's avatar
jurgbohn committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

/* Libre.fm -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Libre.fm Project

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

fauno's avatar
fauno committed
22 23 24 25 26 27
    /*
     * Humanized timestamps
     */

    function human_timestamp ($unix_timestamp, $now = null) {
        if (is_null($now)) {
clint's avatar
clint committed
28
            $now = time();
fauno's avatar
fauno committed
29
        }
clint's avatar
clint committed
30

fauno's avatar
fauno committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        $diff = $now - $unix_timestamp;

        # ugly!
        $number_to_alpha = array(
            'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
            'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty',
            'twenty-one', 'twenty-two', 'twenty-three', 'twenty-four', 'twenty-six', 'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty',
            'thirty-one', 'thirty-two', 'thirty-three', 'thirty-four', 'thirty-five', 'thirty-six', 'thirty-seven', 'thirty-eight', 'thirty-nine',
            'fourty-one', 'fourty-two', 'fourty-three', 'fourty-four', 'fourty-five', 'fourty-six', 'fourty-seven', 'fourty-eight', 'fourty-nine',
            'fifty-one', 'fifty-two', 'fifty-three', 'fifty-four', 'fifty-five', 'fifty-six', 'fifty-seven', 'fifty-eight', 'fifty-nine'
        );

        switch ($unix_timestamp) {
        case ($now < $unix_timestamp):
            return 'in the future (?)';
            break;
        case ($diff == 1):
            # one second
            return 'a second ago';
            break;
        case ($diff < 60):
            # less than a minute
fauno's avatar
fauno committed
53
            return $diff . ' seconds ago';
fauno's avatar
fauno committed
54 55 56 57 58 59 60
            break;
        case ($diff < 120):
            # between a minute and two
            return 'about a minute ago';
            break;
        case ($diff < 3600):
            # less than an hour
fauno's avatar
fauno committed
61
            return round($diff / 60) . ' minutes ago';
fauno's avatar
fauno committed
62 63 64 65 66 67 68
            break;
        case ($diff < 7200):
            # between an hour and two
            return 'about an hour ago';
            break;
        case ($diff < 86400):
            # less than a day
fauno's avatar
fauno committed
69
            return round($diff / 3600) . ' hours ago';
fauno's avatar
fauno committed
70 71 72 73 74 75 76
            break;
        case ($diff < 172800):
            # less than two days
            return 'about a day ago';
            break;
        case ($diff < 604800):
            # less than a week
fauno's avatar
fauno committed
77
            return round($diff / 86400) . ' days ago';
fauno's avatar
fauno committed
78 79 80 81 82 83 84
            break;
        case ($diff < 691200):
            # a week an a day
            return 'about a week ago';
            break;
        case ($diff < 2764800):
            # less than a month
fauno's avatar
fauno committed
85
            return round($diff / 691200) . ' weeks ago';
fauno's avatar
fauno committed
86 87 88 89 90 91 92
            break;
        case ($diff < 4579200):
            # a month and three weeks
            return 'about a month ago';
            break;
        case ($diff < 33177600);
            # less than a year
fauno's avatar
fauno committed
93
            return round($diff / 2764800) . ' months ago';
fauno's avatar
fauno committed
94 95 96 97 98 99 100 101 102 103 104
            break;
        case ($diff < 35942400):
            # a year and a month
            return 'about a year ago';
            break;
        case ($diff > 35942400):
            return 'more than a year ago';
            break;
        }
    }
?>