简体   繁体   中英

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?

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');

我不确定我是否完全理解,但是如果您只想转换字符串,则可以使用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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM