php - PHP 为 Google 调查选择加入代码计算从今天起的天数
问题描述
感谢任何可以提供帮助的人。我正在尝试使用 PHP 来获取从任何给定当天起 X 天的交货日期。这是与 WordPress 中的 Google 调查选择加入代码和 WooCommerce 一起使用的。
引用此线程:Google 调查选择加入代码的 WooCommerce 填写字段
谷歌想要动态值,在这里解释: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');
解决方案
您可以通过以下方式应用此功能,并在代码中添加说明并进行注释。
使用的功能:
date_i18n()
- 基于 Unix 时间戳和时区偏移(以秒为单位)的总和,以本地化格式检索日期。- date - 返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给出时间戳)。换句话说,timestamp 是可选的,默认为 time() 的值。
- Y - 年份的完整数字表示,4 位数字
- m - 月份的数字表示,前导零
- d - 日期,2 位数字,前导零
//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 );
推荐阅读
- javascript - Google Analytics 是否跟踪使用 javascript 更改的图像?
- wpf - WPF ObservableCollection 和 InotifyPropertyChanged
- javascript - React-native 0.61.5 release build android 在特定路线上导航后崩溃
- rstudio - 更改 Rstudio“查找”行为
- javascript - Javascript:在视口中启动 Lottie 动画
- mysql - 尽管语法正确,Nodejs Async await 没有正确等待
- javascript - javascript中正确的数组排序
- java - 无法通过使用带有 Aspose 工作簿的 java 在 ADO(s3 存储桶)中提供密码来取消保护 xlsx、xls 文件
- php - MySQL更新查询join和join left
- jenkins - Jenkins 构建:无法为锁定文件/home/ubuntu/.gradle/.../gradle-6.7.1-all.zip.lck 创建父目录