PHP calculate days from today for Google Survey Opt-In Code

半世苍凉 提交于 2020-08-08 06:07:18

问题


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.

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

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.

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

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

回答1:


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

Functions used:

  • 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

Also used: "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"));

edit: if you don't want today and want an arbitrary date:

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



来源:https://stackoverflow.com/questions/61944153/php-calculate-days-from-today-for-google-survey-opt-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!