首页 > 解决方案 > amp-date-picker 返回不正确的日期

问题描述

使用amp-date-picker并使用它来保存amp-state两者event.startevent.end在所选日期后 1 天返回。

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

<!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>

AMP.printState()
filterState:
    dates: Array(8)
        0: {date: "05/16/2019", id: null}
        1: {date: "05/17/2019", id: null}
        2: {date: "05/18/2019", id: null}
        3: {date: "05/19/2019", id: null}
        4: {date: "05/20/2019", id: null}
        5: {date: "05/21/2019", id: null}
        6: {date: "05/22/2019", id: null}
        7: {date: "05/23/2019", id: null}
    length: 8
    endDate: "05/23/2019"
    startDate: "05/16/2019"

标签: amp-html

解决方案


经过大量搜索,我无法找到为什么我的日期返回不正确,并且发现 AMP 没有日期操作功能。我必须做的是通过创建一个日期编辑器脚本并使用amp-statesrc 调用它来破解系统。

安培代码:

<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);

希望这对将来的某人有所帮助,如果有人找到更好的解决方案,我会听到它。


推荐阅读