PHP get current timestamp

Question

Answers ( 1 )

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

    In PHP, you can get the current timestamp using the time() function. This function returns the current Unix timestamp, which is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

    Here's a simple example of how to use it:

    <?php
    $timestamp = time();
    echo "Current timestamp is: " . $timestamp;
    ?>
    

    This code will display the current timestamp. The time() function is the most common and straightforward way to get the current timestamp in PHP. There aren't many alternative methods to do this in PHP, as time() is the standard function for this purpose. However, if you need the timestamp in a different format, such as a human-readable date, you can use the date() function along with time(), like this:

    <?php
    $timestamp = time();
    echo "Current date and time is: " . date("Y-m-d H:i:s", $timestamp);
    ?>
    

    This code will convert the timestamp into a more readable date and time format, such as "2024-01-12 15:20:30". The date() function takes a format string as its first argument and an optional timestamp as its second argument. When the timestamp is omitted, it uses the current timestamp by default.

Leave an answer