0

I am using laravel imap extension https://www.php-imap.com/api/message And I can get date of message like this

$message->getDate()->toDate();

or

$message->date->toArray()[0]->toArray()['formatted'];

The problem is that date differs in mailbox and the one I get via imap. For example in mailbox date of message is 2022-09-12 11:45 but I get

["date"]=> string(26) "2022-09-12 15:45:34.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" }

Is there any way to get real date that in mailbox?

I've also tries to use pure php imap but I get the same result.

I get mails from different mailboxes so timezones in there can differ, I do not know in advance to which timezone I should switch from UTC

  • It looks like you are getting the real date, but as UTC. What you're seeing in the mailbox is converted to your timezone, which is UTC-4. – aynber Sep 12 '22 at 17:23
  • @aynber Is there a way to unify it somehow? There are cases when I get timezone -7 , +3, 0. It also depends on from whom I get mail. In one mailbox there can be different timezones for a message and if I add or minus digit in timezone I still do not get correct date.. – Emily Carter Sep 12 '22 at 17:30
  • Check the duplicates. It looks like `getDate()` returns a Carbon instance, so you can easily change the timezones. Just know that you can't just set the timezone with `->timezone`, that's just override the offset without changing the time. Use `->setTimezone($timezone)` so that it converts properly. – aynber Sep 12 '22 at 17:31
  • @aynber what if I do not know in advance to witch timezone I should switch? I get mails from different mailboxes and in timezone I get -7, +3, 00:00 – Emily Carter Sep 12 '22 at 17:49
  • It really depends on what you're doing with the timezone and why you want to change it. Usually when I'm changing a timezone, it's to keep it consistent with my own timezone so I don't have to do conversions in my head, or the timezone of a server I work with so the date is correct in that system. – aynber Sep 12 '22 at 17:51
  • @aynber Yes, I need dates like in mailbox so the order of mails was correct. From imap I don't even get timezone like UTC, America/New_York , I just get -7, +3, can I change to correct timezone using this? If I add or substract those hours it does not show correct date like in mailbox – Emily Carter Sep 12 '22 at 17:56
  • -7 and +3 are still valid timezones, they are the offset from UTC. – aynber Sep 12 '22 at 17:58

0 Answers0