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 f开发者_JS百科ormat?
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');
I'm not sure I completely understand, but if you merely want to convert the string, you can use strtotime()
.
$date = gmdate(DATE_RFC822);
Look at Carbon - PHP API extension for DateTime (Link - https://github.com/briannesbitt/Carbon)
$now = Carbon::now('UTC');
You can access individual values as follows
$y = $now->year;
$h = $now->hour; // and so no
While you can use format() to get expected date format as follows
$str_date = $now->format('D, d M Y H:i:s T'); // "Mon, 27 Apr 2015 22:12:30 UTC"
精彩评论