amp-date-picker返回错误的日期

时间:2019-05-13 20:19:22

标签: amp-html

使用amp-date-picker并保存amp-stateevent.start的{​​{1}}时,返回选择日期后1天。

示例:我选择5/15/2019-5/22/2019,它返回5/16/2019-5/23/2019。

event.end

<!doctype html>
<html amp lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <link rel="canonical" href="test.html" hreflang="en-us">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
    <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
    <script async custom-element="amp-date-picker" src="https://cdn.ampproject.org/v0/amp-date-picker-0.1.js"></script>
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  </head>
  <body>
    <amp-state id="filterState">
      <script type="application/json">
      {
        "dates": -1,
        "startDate": -1,
        "endDate": -1
      }
      </script>
    </amp-state>
    <amp-date-picker type="range" mode="static" height="360" format="MM/DD/YYYY" min="" on="select:AMP.setState({ filterState: { dates: event.dates, startDate: event.start, endDate: event.end } })"></amp-date-picker>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

经过大量搜索,我无法找到我的日期返回错误的原因,并且发现AMP没有日期处理功能。我要做的是通过创建日期编辑器脚本并使用amp-state src对其进行调用来入侵系统。

AMP代码:

<amp-state id="filterDates" src="https://www.test.com/date.php" [src]="(filterState.startDate != -1 || filterState.endDate != -1)?'https://www.test.com/date.php?start='+encodeURIComponent(filterState.startDate)+'&end='+encodeURIComponent(filterState.endDate):'https://www.test.com/date.php'"></amp-state>

PHP脚本:

<?php
header('Content-type: application/json');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
header('AMP-Access-Control-Allow-Source-Origin: https://www.test.com');
header('Content-Type: application/json');

$return = array(
    'startDate' => -1,
    'endDate' => -1
);
if(isset($_GET['start']))
{
    $return['startDate'] = date("m/d/Y", strtotime("-1 day", strtotime($_GET['start'])));
}
if(isset($_GET['end']))
{
    $return['endDate'] = date("m/d/Y", strtotime("-1 day", strtotime($_GET['end'])));
}

echo json_encode($return);

希望这对将来的人有所帮助,如果有人找到更好的解决方案,我将为您服务。