感谢任何能帮助我的人。我试图使用PHP来获取距离当前任何给定日期X天的交货日期。这是为了与Google Survey Opt-in代码和WordPress的WooCommerce一起使用。
参考这个帖子。WooCommerce的Google Survey Opt-in代码的填写字段
谷歌要的是动态值,这里解释一下。https:/support.google.commerchantsanswer7106244?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');
你可以用下面的方式来应用,请在代码中加上解释。
使用的函数。
date_i18n()
- 根据Unix时间戳和时区偏移的总和(以秒为单位),以本地化的格式检索日期。//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 );
echo date("Y-m-d", strtotime("+7 day"));
编辑:如果你不想要今天,想要一个任意的日期。
$timestamp = 1590097999;
echo date("Y-m-d", strtotime("+7 day", $timestamp));