PHP从今天开始计算过去的日期
我创建了一个PHP函数来计算WordPress/WooCommerce订单的历史。如果订单超过90天,则应取消订单。该函数用于完美地工作。然而,自2020年新年以来,它已经停止工作。我认为这是因为函数对年份感到困惑,因为从今天起90天是2019年。我如何利用过去几年/2019年进行计算 我尝试过使用WordPress codex中的不同日期格式,而不是mdy。然而,这似乎没有任何区别PHP从今天开始计算过去的日期,php,wordpress,date,woocommerce,Php,Wordpress,Date,Woocommerce,我创建了一个PHP函数来计算WordPress/WooCommerce订单的历史。如果订单超过90天,则应取消订单。该函数用于完美地工作。然而,自2020年新年以来,它已经停止工作。我认为这是因为函数对年份感到困惑,因为从今天起90天是2019年。我如何利用过去几年/2019年进行计算 我尝试过使用WordPress codex中的不同日期格式,而不是mdy。然而,这似乎没有任何区别 function expire_after_x_days(){ global $wpdb; //
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("m/d/y");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("m/d/y", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)) foreach ($result as $order){
// Get order's time
$order_time = get_the_time('m/d/y', $order->ID );
// Compare order's time with current time
if ( $order_time < $expiration_date ){
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
函数在\u x\u天后过期(){
全球$wpdb;
//获取当前时间
$today=日期(“m/d/y”);
//设定到期时间
$time_to_expire=“-90天”;
$expiration_date=日期(“m/d/y”,标准时间($today.$time_to_expire));
//获取具有处理状态的订单
$result=$wpdb->get_results(“从$wpdb->post中选择*,其中post_类型='shop_order',post_状态='wc processing'”);
如果(!empty($result))foreach($result作为$order){
//拿到订单的时间
$order\u time=获取\u时间('m/d/y',$order->ID);
//将订单时间与当前时间进行比较
如果($订单\时间<$到期\日期){
//更新订单状态
$orders=array();
$orders['ID']=$order->ID;
$orders['post_status']='wc cancelled';
wp_更新_职位(订单);
}
}
}
添加操作('admin_footer','expire_after_x_days');
请将日期格式从m/d/y更改为y-m-d。请参阅下面的代码
您还可以通过修改$order_time='12/11/18'手动进行检查
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("Y-m-d");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("Y-m-d", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)){
foreach ($result as $order){
// Get order's time
$order_time = get_the_time('Y-m-d', $order->ID );
// Compare order's time with current time
//$order_time = '12/11/18';
if ( $order_time < $expiration_date ){
//die("olde");
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}else{
//echo 'not old date';die;
}
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
函数在\u x\u天后过期(){
全球$wpdb;
//获取当前时间
$today=日期(“Y-m-d”);
//设定到期时间
$time_to_expire=“-90天”;
$expiration_date=日期(“Y-m-d”,标准时间($time_to_expire));
//获取具有处理状态的订单
$result=$wpdb->get_results(“从$wpdb->post中选择*,其中post_类型='shop_order',post_状态='wc processing'”);
如果(!空($result)){
foreach($订单的结果){
//拿到订单的时间
$order\u time=获取\u时间($Y-m-d',$order->ID);
//将订单时间与当前时间进行比较
//$order_time='12/11/18';
如果($订单\时间<$到期\日期){
//死亡(“旧”);
//更新订单状态
$orders=array();
$orders['ID']=$order->ID;
$orders['post_status']='wc cancelled';
wp_更新_职位(订单);
}否则{
//呼应“不老的日期”;死亡;
}
}
}
}
添加操作('admin_footer','expire_after_x_days');
请将日期格式从m/d/y更改为y-m-d。请参阅下面的代码
您还可以通过修改$order_time='12/11/18'手动进行检查
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("Y-m-d");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("Y-m-d", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)){
foreach ($result as $order){
// Get order's time
$order_time = get_the_time('Y-m-d', $order->ID );
// Compare order's time with current time
//$order_time = '12/11/18';
if ( $order_time < $expiration_date ){
//die("olde");
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}else{
//echo 'not old date';die;
}
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
函数在\u x\u天后过期(){
全球$wpdb;
//获取当前时间
$today=日期(“Y-m-d”);
//设定到期时间
$time_to_expire=“-90天”;
$expiration_date=日期(“Y-m-d”,标准时间($time_to_expire));
//获取具有处理状态的订单
$result=$wpdb->get_results(“从$wpdb->post中选择*,其中post_类型='shop_order',post_状态='wc processing'”);
如果(!空($result)){
foreach($订单的结果){
//拿到订单的时间
$order\u time=获取\u时间($Y-m-d',$order->ID);
//将订单时间与当前时间进行比较
//$order_time='12/11/18';
如果($订单\时间<$到期\日期){
//死亡(“旧”);
//更新订单状态
$orders=array();
$orders['ID']=$order->ID;
$orders['post_status']='wc cancelled';
wp_更新_职位(订单);
}否则{
//呼应“不老的日期”;死亡;
}
}
}
}
添加操作('admin_footer','expire_after_x_days');
您将这些变量视为日期时间实例,但它们是字符串。此$order\u time<$expiration\u date
按字母顺序比较字符串,而不是按其日期含义进行比较。请改用DateTime类()。您将这些变量视为DateTime实例,但它们是字符串。此$order\u time<$expiration\u date
按字母顺序比较字符串,而不是按其日期含义进行比较。改用DateTime类()。通过使用WHERE
子句运行UPDATE
查询,只获取那些超过90天的订单,可以大大简化这一过程。不需要将它们全部提取出来并循环结果
您需要将post_created
设置为列的实际名称
function expire_after_x_days() {
global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts
SET post_status = 'wc-cancelled'
WHERE post_type = 'shop_order'
AND post_status = 'wc-processing'
AND post_created < DATE_SUB(NOW(), INTERVAL 90 DAY)");
}
函数在\u x\u天后过期(){
全球$wpdb;
$result=$wpdb->query(“更新$wpdb->posts
设置post_状态='wc已取消'
其中post_类型='shop_order'
和post_状态='wc处理'
和post_created
您可以通过使用WHERE
子句运行UPDATE
查询来简化这一过程,只获取那些超过90天的订单。不需要将它们全部提取出来并循环结果
您需要将post_created
设置为列的实际名称
function expire_after_x_days() {
global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts
SET post_status = 'wc-cancelled'
WHERE post_type = 'shop_order'
AND post_status = 'wc-processing'
AND post_created < DATE_SUB(NOW(), INTERVAL 90 DAY)");
}
函数在\u x\u天后过期(){
全球$wpdb;
$result=$wpdb->query(“更新$wpdb->posts
设置post_状态='wc已取消'