【问题标题】:PHP calculate days from today for Google Survey Opt-In CodePHP 从今天开始计算 Google 调查选择加入代码的天数
【发布时间】:2020-05-21 21:29:33
【问题描述】:

感谢任何可以提供帮助的人。我正在尝试使用 PHP 来获取从任何给定当天起 X 天的交货日期。这是与 WordPress 中的 Google 调查选择加入代码和 WooCommerce 一起使用的。

引用本帖:WooCommerce fill-in fields for Google Survey Opt-In Code

Google 想要动态值,此处解释:https://support.google.com/merchants/answer/7106244?hl=en&ref_topic=7105160#example

我已经准备好大部分代码,但是这个动态日期很难弄清楚。

我认为最简单的解决方案是在产品订单的当天添加天数,这可能发生在任何一天。

我的问题是:我如何让 PHP 在这种情况下计算它?

我的理解是有 DateTime 和 strtotime,但是 DateTime 是更新和“正确”的方法吗?

这是我目前得到的,但我不确定它是否正确:

    //Google Survey code 
function wh_CustomReadOrder($order_id) {
    //getting order object
    $order = wc_get_order($order_id);
    $email = $order->billing_email;
    ?>
    <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
    <script>
        window.renderOptIn = function () {
            window.gapi.load('surveyoptin', function () {
                window.gapi.surveyoptin.render(
                        {
                            "merchant_id": [merchant id],
                            "order_id": "<?php echo $order_id; ?>",
                            "email": "<?php echo $email; ?>",
                            "delivery_country": "CA",
                            "estimated_delivery_date": "<?php
$inOneWeek = new \DateTime("+7 day");
echo $date->format("Y-m-d");
?>"
                        }
                );
            });
        };
    </script>
    <?php
}

add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

【问题讨论】:

  • 问题的状态如何?通过给出的答案解决了吗?如果是这样,您可能希望通过接受将您的问题标记为已解决。

标签: php wordpress date woocommerce future


【解决方案1】:

您可以通过以下方式应用它,注释并在代码中添加解释。

使用的功能:

  • date_i18n() - 根据 Unix 时间戳和时区偏移(以秒为单位)之和,以本地化格式检索日期。
  • date - 返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给出时间戳)。换句话说,timestamp 是可选的,默认为 time() 的值。
    • Y - 年份的完整数字表示,4 位数字
    • m - 月份的数字表示,前导零
    • d - 日期,2 位数字,前导零

也用过:"How to get WooCommerce order details"


//Google Survey code 
function wh_CustomReadOrder($order_id) {
    // Get order object
    $order = wc_get_order($order_id);

    // Get billing email
    $email = $order->get_billing_email();

    // Get order date
    $date_created = $order->get_date_created();

    // Add days
    $days = 7;

    // Date created + 7 days
    $estimated_delivery_date = date_i18n( 'Y-m-d', strtotime( $date_created ) + ( $days * 24 * 60 * 60 ) );

    ?>
    <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
    <script>
    window.renderOptIn = function () {
        window.gapi.load('surveyoptin', function () {
            window.gapi.surveyoptin.render({
                "merchant_id": [merchant id],
                "order_id": "<?php echo $order_id; ?>",
                "email": "<?php echo $email; ?>",
                "delivery_country": "CA",
                "estimated_delivery_date": "<?php echo $estimated_delivery_date; ?>"
            });
        });
    };
    </script>
    <?php   
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder', 10, 1 );

【讨论】:

    【解决方案2】:

    echo date("Y-m-d", strtotime("+7 day"));

    编辑:如果您不想要今天并且想要任意日期:

    $timestamp = 1590097999; echo date("Y-m-d", strtotime("+7 day", $timestamp));

    【讨论】:

    • 嗨,我很好奇“1590097999”是干什么用的?它如何创建任意日期?只是想了解这个编码世界。感谢您的时间。这也是针对哪个编辑的?
    • 1590097999 只是一个整数。您可以从 time() 获取当前时间,也可以从数据库、文件或任何来源获取当前时间。它只需要一个数字,表示从 1970-1-1 到该日期所经过的秒数。
    猜你喜欢
    • 2016-09-07
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多