根据时区将日期转换为时间戳

Converting date to timestamp based on timezone

我正在使用 PHP Imap 获取电子邮件,这是一个对象示例:

Array
(
    [0] => stdClass Object
        (
            [subject] => Email Subject
            [from] => Sender <sender@domain.com>
            [to] => me@domain.com
            [date] => Sat, 19 Aug 2017 20:09:33 +1000
            [message_id] => <80d657c74967c8dc56138ca9552f0a2e@anyweb.apca.local>
            [size] => 1881518
            [uid] => 703
            [msgno] => 527
            [recent] => 0
            [flagged] => 0
            [answered] => 0
            [deleted] => 0
            [seen] => 0
            [draft] => 0
            [udate] => 1503137430
        )

)

虽然我有 udate 但我想仔细检查是否与我的时区匹配,所以我做了:

date_default_timezone_set('Australia/Melbourne');
$str = 'Sat, 19 Aug 2017 20:09:33 +1000';
echo strtotime($str); // 1503137373 ??

甚至尝试过:

$date = new DateTime($str, new DateTimeZone('Australia/Melbourne'));
$timestamp = $date->format('U');
echo $timestamp; // 1503137373 ?? 

所以在这两种情况下,我得到的时间戳与从邮件服务器获取的时间戳不匹配,我在这里错过了什么?

  • udate - 由邮件服务器标记

  • 日期,日期 - 客户标注

The difference between 'date' and 'udate' seems to be rather more than just the way they're formatted.

'date' is the date that was written in the headers by the sender's mail client, and probably bears little to do with reality. It's dependent on your sender knowing what the correct time is; it could be out by a few minutes, days, months or even years.

'udate' is the real date that the e-mail hit your IMAP server.

Use 'udate' if you want to do neat stuff like work out how much e-mail you get sent on a daily basis - or, as I do, how much spam I get.

http://titanic.fauser.edu/php/function.imap-headerinfo.php.htm


来自here

date - The message date as found in its headers

Date - Same as date

udate - mail message date in Unix time

你可以这样做

date_default_timezone_set('Australia/Melbourne');

$script_tz = date_default_timezone_get();

if (strcmp($script_tz, ini_get('date.timezone'))){
   echo 'Script timezone differs from ini-set timezone.';
} else {
   echo 'Script timezone and ini-set timezone match.';
}