Search code examples
phpdatetimezoneimap

Converting date to timestamp based on timezone


I'm fetching emails using PHP Imap and this is an example of an object:

Array
(
    [0] => stdClass Object
        (
            [subject] => Email Subject
            [from] => Sender <[email protected]>
            [to] => [email protected]
            [date] => Sat, 19 Aug 2017 20:09:33 +1000
            [message_id] => <[email protected]>
            [size] => 1881518
            [uid] => 703
            [msgno] => 527
            [recent] => 0
            [flagged] => 0
            [answered] => 0
            [deleted] => 0
            [seen] => 0
            [draft] => 0
            [udate] => 1503137430
        )

)

Although I do have a udate but I wanted to double check if matches my timezone, so I did:

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

Even tried:

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

So in both cases I get a timestamp that doesn't match the one thats fetched from the mail server, what am I missing here?


Solution

  • you can do something like this

    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.';
    }