PHP 从今天开始计算 Google 调查选择加入代码的天数

joy*_*ace 2 php wordpress future date woocommerce

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

参考此线程:WooCommerce 填写 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');
Run Code Online (Sandbox Code Playgroud)

7uc*_*f3r 5

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

使用的功能:

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

还使用:“如何获取 WooCommerce 订单详细信息”


//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 );
Run Code Online (Sandbox Code Playgroud)