如何从php中的旧时间字符串中找到区域的当前时间

  • 本文关键字:时间 区域 php 字符串 php time
  • 更新时间 :
  • 英文 :


我正在从某些邮件date标题中获取时间字符串。我需要在将邮件发送给用户之前得到多少秒。

这是我试图解决问题的代码。

<?php
//time string found in mail date header
$time_string = 'Wed, 15 Nov 2017 14:50:53 -0800 (PST)';
//when i use 'D, j M Y G:i:s O (e)' as identifier then date_create_from_format returns false
$mail_date = explode(" ", $time_string);
unset($mail_date[6]);
$dt_in = date_create_from_format('D, j M Y G:i:s O', implode(" ", $mail_date));
$time_in = strtotime(date_format($dt_in, 'd-m-Y H:i:s')) ; // This time may be according to my time zone (that is wrong need according to that time zone '-0800')
//returns time zone that is not compatible with php
$time_zone = date_format($dt_in, 'T'); //returns GMT-0800
//rest of the code not working
date_default_timezone_set($time_zone); //date_default_timezone_set(): Timezone ID 'GMT-0800' is invalid
$difference = time()-$time_in;
print_r($difference);
?>

纠正此代码或这样做是不起作用的。我需要获得时差。任何解决方案都将是可观的。预先感谢。

将日期从电子邮件转换为时间戳,并与当前时间戳进行比较:

$time_string = 'Wed, 15 Nov 2017 14:50:53 -0800 (PST)';
$mail_date = explode(" ", $time_string);
unset($mail_date[6]);
$mail_datetime = date_create_from_format('D, j M Y G:i:s O', implode(" ", $mail_date));
$time_diff_in_seconds = time() - $mail_datetime->getTimestamp();

最新更新