php-imap
php-imap copied to clipboard
Getting wrong date (time zone)
Hi) I'm getting messages like this
$messages = $folder->messages()->unseen()->all()->get();
And get date like this
https://prnt.sc/F-bEO3NWS3v3
[date] => Webklex\PHPIMAP\Attribute Object
(
[name:protected] => date
[values:protected] => Array
(
[0] => Carbon\Carbon Object
(
[endOfTime:protected] =>
[startOfTime:protected] =>
[constructedObjectId:protected] => 0000000048718c42000000004c213cc5
[localMonthsOverflow:protected] =>
[localYearsOverflow:protected] =>
[localStrictModeEnabled:protected] =>
[localHumanDiffOptions:protected] =>
[localToStringFormat:protected] =>
[localSerializer:protected] =>
[localMacros:protected] =>
[localGenericMacros:protected] =>
[localFormatFunction:protected] =>
[localTranslator:protected] =>
[dumpProperties:protected] => Array
(
[0] => date
[1] => timezone_type
[2] => timezone
)
[dumpLocale:protected] =>
[dumpDateProperties:protected] =>
[date] => 2022-09-11 09:02:20.000000
[timezone_type] => 1
[timezone] => +03:00
)
)
)
The problem is that date is not the same as in mailbox. There could be difference in 1 hour (outlook) or 7 hours(privateeemail) depending on mailbox.
For example here the date is like this https://prnt.sc/SfP08a-rEOsC and instead of 2:02 I get 09:02..
I guess there is a problem with time zone?
Is there a way to get real date from mailbox? If so could you please provide an example how to do it?
@Webklex There is update. From one mailbox there can be different time zones depending on from whom a letter was received.
["date"]=>
string(26) "2022-09-12 16:42:23.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+03:00"
and
["date"]=>
string(26) "2022-09-12 06:50:43.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "-07:00"
Is there any way to get real date that is in mailbox in the letter?
@Webklex I've tried using timezone
var_dump( $message->date->toArray()[0]->toArray()['formatted']);
var_dump($message->date->toArray()[0]->toArray()['timezone']->__toString());
$newDate = Carbon::createFromFormat('Y-m-d H:i:s', $message->date->toArray()[0]->toArray()['formatted']);
$newDate = $newDate->setTimezone($message->date->toArray()[0]->toArray()['timezone']->__toString());
var_dump($newDate->format("Y-m-d H:i:s"));
But date is still different. in mailbox 11:38, from imap 8:38, after using timezone 1:38. The order of mails is broken because of wrong dates)
Hi @jane08 ,
if you dump the raw message header $message->getHeader()->raw and look for a line starting with Date: what does it say?
If you want to change the timezone of a date, you can call $message->date->first()->timezone('UTC'); to do so.
Best regards & happy coding,
@Webklex I get Date: Mon, 5 Sep 2022 21:24:00 +0300
In mailbox it is 9/5/2022 2:24 PM
It is converted to another timezone by default when I get letters. And even if I convert it to UTC the order by date on my site and in maibox is different..
I just need letters ordered the same way as in mailbox..
Are letters in maibox sorted by date? It seems like When I get dates in UTC order gets broken, letters that should be earlier by date are later..
The only thing I was able to do is get all messages and sort them by id desc from database
Hi @jane08 , thanks for the follow up. Looking through all the reported dates, it looks like you are mixing them up.
Lets look at one message at the time. If you pick one message:
- whats the time of your server where you are running this script
- whats the time of your application running this script
- whats the raw / original email time (The line starting with "Date: " inside the output of
$message->getHeader()->raw) - whats the time you want it to be? How far is it off? Which timezone is the date in and which timezone should it be in?
Best regards,
P.s.: regarding sorting: the result is sorted by the email provider. Usually they are sorted by message number or uid. If you want to sort the result, you can do by utilizing the collection api, which is well described here: https://laravel.com/docs/9.x/collections#method-sort
$messages = $folder->messages()->unseen()->all()->get()->sortBy("something");
@Webklex thank you)
Getting messages and then sorting by id desc from db seems like working at the moment.