Search code examples
phpdatetimeutc

In PHP: How to store the current time/date in UTC format in a string?


example:

$date = 'Wed, 18 Feb 2009 16:03:52 GMT';

//How can I get $date to equal the current time in the same format?


Solution

  • I assume you mean how to get similar format as that? As there is no exact match in the predefined date constants, this would do it:

    $date = gmdate('D, j M Y H:i:s e');
    

    That would return the current date and time in the same format as 'Wed, 18 Feb 2009 16:03:52 GMT'.

    EDIT

    GMT and UTC are (in normal cases) completely interchangeable, and as gmdate always returns an GMT/UTC date, you can just use this:

    $date = gmdate('D, j M Y H:i:s').' GMT';
    

    Or, as it turns out, you can replace e with T to get GMT:

    $date = gmdate('D, j M Y H:i:s T');