PHP从今天开始计算Google Survey加入代码的天数 [英] PHP calculate days from today for Google Survey Opt-In Code

查看:51
本文介绍了PHP从今天开始计算Google Survey加入代码的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢任何可以提供帮助的人。我正在尝试使用PHP来获取从任何给定日期算起X天的交货日期。

Thank you to anyone who can help. I'm trying to use PHP to get a delivery date that is X days from any given current day. This is to use with the Google Survey Opt-in code and WooCommerce in WordPress.

此引用与该线程一起使用: Google Survey Opt-In Code的WooCommerce填充字段

Referencing this thread: WooCommerce fill-in fields for Google Survey Opt-In Code

Google需要动态值,在这里进行了说明: https://support.google.com/merchants/answer/7106244?hl=zh-CN&ref_topic=7105160#example

Google wants dynamic values, explained here: https://support.google.com/merchants/answer/7106244?hl=en&ref_topic=7105160#example

我拥有大部分代码可以使用了,但是这个动态的日期很难弄清。

I have most of the code ready to go, but this dynamic date has been hard to figure out.

我认为最简单的解决方案是在产品的某天增加几天

I think the simplest solution is to just add a number of days to the day of a product order, which can happen on any given day.

我的问题是:在这种情况下,如何获取PHP进行计算?

My question is: how do I get PHP to calculate that in this context?

我的理解是,有DateTime和strtotime,bu t DateTime是更新的正确方法吗?

My understanding is that there is DateTime and there is strtotime, but DateTime is the more recent and 'right' way to do this?

这是我到目前为止得到的,但是我不确定这是正确的:

This is what I've got so far, but I'm not sure it's right:

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


推荐答案

您可以按以下方式应用它,并用

You could apply this in the following way, comment with explanation added in the code.

使用的功能:


  • date_i18n() -检索本地化日期格式,基于Unix时间戳和时区偏移量(以秒为单位)。

  • 日期-返回一个字符串,该字符串根据给定的格式字符串使用给定的整数时间戳或当前时间(如果未提供时间戳)进行格式化。换句话说,时间戳是可选的,默认为time()的值。


    • Y-一年的全数字表示,四位数

    • m-一个月的数字表示,其中前导零

    • d-月份中的第二天,前两位为零

    • date_i18n() - Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
    • date - Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
      • Y - A full numeric representation of a year, 4 digits
      • m - Numeric representation of a month, with leading zeros
      • d - Day of the month, 2 digits with leading zeros

      也使用:如何获取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 );
      

      这篇关于PHP从今天开始计算Google Survey加入代码的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆