分解发布的数据以在php中接收日期

时间:2019-02-17 03:12:12

标签: php datetime explode

我正在将进入域的电子邮件转发到php脚本。它很大,我不知道为什么stackoverflow不允许我发布完整长度,因为它给出了错误this looks like spam.

但是,它的日期部分看起来像

.......\nFrom: Firstname Lastname <name@gmail.com>\nDate: Sun, 17 Feb 2019 08:09:37 +0545\nMessage-ID: <abcdefghifsfsasd@mail.gmail.com>\nSubject:.......

我的目的是获取此电子邮件的接收日期时间。

我在那里可以看到Date: Sun, 17 Feb 2019 08:09:37 +0545。这个日期对我来说是必需的日期时间。如何获得?

我尝试用preg_split('/\r\n|\r|\n/', $_POST['headers']);爆炸,但随后我无法获得Idea,因为它似乎是一系列字符串。

"Received: by mx0035p1mdw1.sendgrid.net with SMTP id ADiBRHCowl Sun, 17 Feb 2019 02:59:38 +0000 (UTC)",
   "Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by mx0035p1mdw1.sendgrid.net (Postfix) with ESMTPS id DB6E7A05D27 for <email@domain.com>; Sun, 17 Feb 2019 02:59:37 +0000 (UTC)",
   "Received: by mail-ed1-f48.google.com with SMTP id m35so7028682ede.10 for <email@domain.com>; Sat, 16 Feb 2019 18:59:37 -0800 (PST)",
   "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed\/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=ttLV0Sk78+4DRG2CM0f\/kiWFXPnwCHm0Y\/1zM9lMrRo=; b=Dgyv4S\/TDdo61RsYchmhkFnFtG8VksVIeAimm7WOnHSv6chN+LOG25ufsh4\/51xX1Z pjQvuiXnuR7MawZ0B\/WZxB4F4309XT9SIrZMBX9\/5HHd\/SddFx1LZ6XVS2qEvaVV9B\/9 4v\/wqNhnNr7Vy1GlKbUq6buB9l4fk9J+ralDNF6lrDbP1XVCv+58Qp0UipMLPzYmFGnf EtylH7N4vVNoOjww15MwT5xrgW3ylfSJLZXog3TBxK+v8fGFKQ0UOH\/+C5PL0sQaFVM5 EVGtwQgqbBeEhJnG77xVuApcerYNYY+gM1Sixjb4NhRtsKgE5fOTAzA3N+jXbDIVunsZ F45g==",
   "X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed\/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ttLV0Sk78+4DRG2CM0f\/kiWFXPnwCHm0Y\/1zM9lMrRo=; b=s\/ovc9XPMypTBh6anKxw1WFQRJ3I4AGVFaQdu9L0buU2m8tl2JFPLWOYeliRS5K\/Jd WLiTXfRdFw8OpAWuKUbMt0wQ+gZXd0AwpJb\/t5KFs+Fh7l0cjWRSK\/0ZzADqagoyDmhC ISSRazoYBu2h9IeWdOhFV4i+VBvyhkXqauggB6kvZxG4GM\/ulB7sj5zYajnVBt5SfSI1 9SHMkkZrYerI6mCAKgqr\/eEj8E0IYgaKlhct9TlZbh9wsmYUQoDzp4Wf9JET5jhL7Lya 3Pa41NZepc03HfGOcnYadrPfrIz7O2Tm++22D0LBTzozkskwlJqqZz\/rLf0Osu81q80h VdEg==",
   "X-Gm-Message-State: AHQUAuaQnQxerRWkc4sbGZmEt9JioFURi6LQDVYBzb60RKG+21LzjDlV OgAylSy0nmPPAEI7A\/Faxook200gMFui+BFD8sWrBg==",
   "X-Google-Smtp-Source: AHgI3IZGrcH6ngLPOL8YS8PuOU6OPlSk2Q2DDeKmX41lGFBsvhtZ2FFtGkGypCSGCeVJd\/wP6i+pi2S6v5EIJaZOxNA=",
   "X-Received: by 2002:aa7:dacd:: with SMTP id x13mr13739830eds.24.1550372376434; Sat, 16 Feb 2019 18:59:36 -0800 (PST)",
   "MIME-Version: 1.0",
   "From: Firstname Lastname <name@gmail.com>",
   "Date: Sun, 17 Feb 2019 08:44:24 +0545",
   "Message-ID: <CACX9xtmKSuZYgnXXEq-z=jtBwG4DG=ytptVVCOsY7SEOi2o_KQ@mail.gmail.com>",
   "Subject: dummy12",
   "To: email@domain.com",
   "Content-Type: multipart\/alternative; boundary=\"000000000000c45c6e05820e32b0\"",
   ""

如何从此行"Date: Sun, 17 Feb 2019 08:44:24 +0545"

获取日期

更新:

在preg_split $res = preg_split('/\r\n|\r|\n/', $_POST['headers']);

之后
if(count($res)>0){
  $output = $res[10];
}

这时$ output是Date: Sun, 17 Feb 2019 08:09:37 +0545

3 个答案:

答案 0 :(得分:0)

由于电子邮件标题相当一致(并且数据格式始终保持一致),因此可以使用RegEx模式匹配日期值。

$matches = null;
$returnValue = preg_match('/"Date: (.+?)",/', $headers_data, $matches);

$ matches将是一个包含两个元素(如果成功)的数组:匹配的子字符串总数和捕获的日期数据。您可能会对后者最感兴趣,该网址为:

$matches[1]

有关preg_match的更多信息,请查看PHP手册。

答案 1 :(得分:0)

我已经找到了解决方案,可能不是最好的,但是它正在完成任务。

$f_header = preg_split('/\r\n|\r|\n/', $_POST['headers']);
if(count($f_header)>0){
    $output = substr("$f_header[10]",6);
    $date = explode("+",$output);
    $dt = date('Y-m-d h:i:s', strtotime($date[0]));
}

答案 2 :(得分:0)

我发现了:

$dt = date('Y-m-d h:i:s', strtotime(substr($str, strpos($str, 'Date')+6, 31)));