convert timestamp to date in PHP.

Question

Answers ( 1 )

    0
    2024-01-11T18:05:23+00:00

    In PHP, you can convert a timestamp to a readable date format using the date() function. The date() function formats a local date and time, and accepts two parameters - the format of the outputted date string and the optional Unix timestamp.

    Here's an example of how you can convert a timestamp to a date:

    <?php
    $timestamp = 1633072800; // Example timestamp
    echo date("Y-m-d H:i:s", $timestamp);
    ?>
    

    In this example, "Y-m-d H:i:s" is the format string. This will display the date in the format of year-month-day hour:minute:second. You can modify the format string according to your needs. For example, if you only need the date, you can use "Y-m-d".

    PHP offers a variety of characters that can be used in the format string to represent different date and time components. Some commonly used ones are:

    • Y: A full numeric representation of a year, 4 digits
    • m: Numeric representation of a month, with leading zeros
    • d: Day of the month, 2 digits with leading zeros
    • H: 24-hour format of an hour with leading zeros
    • i: Minutes with leading zeros
    • s: Seconds, with leading zeros

    Remember that the timestamp should be in the Unix format, which is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Leave an answer