PHP get timestamp

Question

Answers ( 1 )

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

    In PHP, you can obtain the current timestamp using the time() function. This function returns the current Unix timestamp, which is the number of seconds since January 1, 1970 (also known as the Unix epoch). Here's a basic example:

    <?php
    $timestamp = time();
    echo $timestamp;
    ?>
    

    This code will display the current timestamp.

    If you need a timestamp for a specific date and time, you can use the strtotime() function. This function takes a date string as input and returns the corresponding timestamp. For example:

    <?php
    $timestamp = strtotime("2024-01-12 12:00:00");
    echo $timestamp;
    ?>
    

    This will output the timestamp for January 12, 2024, at 12:00 PM.

    PHP also provides the DateTime class for more complex date and time operations. You can get the current timestamp using this class as well:

    <?php
    $date = new DateTime();
    $timestamp = $date->getTimestamp();
    echo $timestamp;
    ?>
    

    These are the common ways to obtain a timestamp in PHP. Each method has its use cases, depending on your specific requirements.

Leave an answer