Home > Backend Development > PHP Tutorial > How Can I Generate Relative Date/Time Strings from Timestamps in PHP?

How Can I Generate Relative Date/Time Strings from Timestamps in PHP?

Susan Sarandon
Release: 2024-12-12 20:03:17
Original
499 people have browsed it

How Can I Generate Relative Date/Time Strings from Timestamps in PHP?

PHP: Generating Relative Date/Times from Timestamps

In PHP, converting a timestamp to a relative date/time string that accurately represents time differences for both past and future timestamps can be a challenging task.

Relative Date/Time Format

The desired format of the relative date/time string should include:

  • Past dates: "x weeks ago", "y days ago", etc.
  • Future dates: "in x minutes", "after y days", etc.
  • Present tense (for negligibly small differences): "now" or "just now"

Solution

The following function, time2str, efficiently generates relative date/time strings:

function time2str($ts)
{
    // Convert to timestamp if not already
    if (!ctype_digit($ts)) {
        $ts = strtotime($ts);
    }

    // Calculate difference between current time and timestamp
    $diff = time() - $ts;

    // Past Timestamps
    if ($diff > 0) {
        $day_diff = floor($diff / 86400);
        switch ($day_diff) {
            case 0:
                if ($diff < 60) {
                    return 'just now';
                } elseif ($diff < 120) {
                    return '1 minute ago';
                } elseif ($diff < 3600) {
                    return floor($diff / 60) . ' minutes ago';
                } elseif ($diff < 7200) {
                    return '1 hour ago';
                } elseif ($diff < 86400) {
                    return floor($diff / 3600) . ' hours ago';
                }
                break;
            case 1:
                return 'Yesterday';
            default:
                if ($day_diff < 7) {
                    return $day_diff . ' days ago';
                } elseif ($day_diff < 31) {
                    return ceil($day_diff / 7) . ' weeks ago';
                } elseif ($day_diff < 60) {
                    return 'last month';
                } else {
                    return date('F Y', $ts);
                }
        }
    }

    // Future Timestamps
    else {
        $diff = abs($diff);
        $day_diff = floor($diff / 86400);
        switch ($day_diff) {
            case 0:
                if ($diff < 120) {
                    return 'in a minute';
                } elseif ($diff < 3600) {
                    return 'in ' . floor($diff / 60) . ' minutes';
                } elseif ($diff < 7200) {
                    return 'in an hour';
                } elseif ($diff < 86400) {
                    return 'in ' . floor($diff / 3600) . ' hours';
                }
                break;
            case 1:
                return 'Tomorrow';
            default:
                if ($day_diff < 4) {
                    return date('l', $ts);
                } elseif ($day_diff < 7 + (7 - date('w'))) {
                    return 'next week';
                } elseif (ceil($day_diff / 7) < 4) {
                    return 'in ' . ceil($day_diff / 7) . ' weeks';
                } elseif (date('n', $ts) == date('n') + 1) {
                    return 'next month';
                } else {
                    return date('F Y', $ts);
                }
        }
    }
}
Copy after login

Usage Example

echo time2str('2023-09-20 10:00:00'); // Output: "in 27 days"
echo time2str('2022-09-20 10:00:00'); // Output: "last year"
echo time2str('2022-09-20T10:00:00+00:00'); // Output: "last year"
Copy after login

The above is the detailed content of How Can I Generate Relative Date/Time Strings from Timestamps in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template