if-modified-since的PHP时间戳,带有时区

时间:2015-09-22 04:49:15

标签: php datetime timestamp if-modified-since

我有以下列格式存储在MySQL中的时间戳:

$if_modified_since = date('Y-m-d H:i:s');

使用服务器的默认时区保存时间戳,在我的情况下为EST。我需要使用CURL标头发送if-modified-since请求。

curl_setopt($ch, CURLOPT_HTTPHEADER, array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',$if_modified_since)));

EST的时间转换为GMT的最佳方式是什么?还有办法通过检查服务器时区(而不仅仅是EST中的硬编码)使其完全通用吗?

1 个答案:

答案 0 :(得分:0)

这对我有用:

$d = DateTime::createFromFormat('Y-m-d H:i:s', $if_modified_since, new DateTimeZone(ini_get('date.timezone'))); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',$d->getTimestamp())));