Home > Backend Development > PHP Tutorial > How to Convert Timestamps to Relative Date/Time in PHP?

How to Convert Timestamps to Relative Date/Time in PHP?

Patricia Arquette
Release: 2024-11-28 00:00:16
Original
372 people have browsed it

How to Convert Timestamps to Relative Date/Time in PHP?

PHP: Converting Timestamps to Relative Date/Time

Relative timestamps indicate the time elapsed between a given point in time and the current moment. They provide a more user-friendly representation of time than absolute timestamps, especially for events that occurred in the past or will occur in the future.

To generate relative timestamps in PHP, consider using the following function:

function time2str($ts)
{
    if (!ctype_digit($ts)) {
        $ts = strtotime($ts);
    }

    $diff = time() - $ts;
    if ($diff == 0) {
        return 'now';
    } elseif ($diff > 0) {
        // Past timestamps
        $day_diff = floor($diff / 86400);
        if ($day_diff == 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';
            }
        } elseif ($day_diff == 1) {
            return 'Yesterday';
        } elseif ($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);
        }
    } else {
        // Future timestamps
        $diff = abs($diff);
        $day_diff = floor($diff / 86400);
        if ($day_diff == 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';
            }
        } elseif ($day_diff == 1) {
            return 'Tomorrow';
        } elseif ($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

This function accepts a timestamp and outputs a relative timestamp. It handles both past and future timestamps, and it provides outputs in a variety of formats, such as "1 hour ago" or "in 2 days."

The above is the detailed content of How to Convert Timestamps to Relative Date/Time 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