PHP time conversion to timestamp

Question

Answers ( 1 )

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

    In PHP, you can convert a date or time string to a timestamp using the strtotime() function. This function parses an English textual datetime description into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

    Here's an example of how to use strtotime():

    $date = "2024-01-12 10:00:00"; // Example date
    $timestamp = strtotime($date);
    
    echo $timestamp; // This will output the Unix timestamp for the given date
    

    This is the most common method for converting a standard date and time format to a timestamp in PHP. If you have a different format, you might need to use the DateTime class to create a more flexible conversion, like this:

    $date = "2024-01-12 10:00:00"; // Example date
    $dateTime = new DateTime($date);
    $timestamp = $dateTime->getTimestamp();
    
    echo $timestamp; // Outputs the Unix timestamp
    

    These methods should cover most needs for converting dates and times to timestamps in PHP.

Leave an answer