【问题标题】:PHP calculate past dates from todayPHP计算从今天开始的过去日期
【发布时间】:2020-02-01 10:20:36
【问题描述】:

我创建了一个 PHP 函数来计算 WordPress/WooCommerce 订单的年龄。如果订单超过 90 天,则应取消。该功能曾经完美运行。但是,自 2020 年新年以来,它已停止工作。我认为这是因为该函数对年份感到困惑,因为从今天起 -90 天是 2019 年。如何使计算与过去几年/2019 年一起工作?

我尝试过使用 WordPress codex 而不是 mdy 中的不同日期格式。但是,这似乎没有任何区别。

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

【问题讨论】:

  • 我试过你在本地编码,一切正常......
  • @Simone Rossaini 当我点击几次后端时,我的所有订单都被取消了(也是今天的新订单)。这只是在新的一年之后才开始的。我已经检查了插件冲突等问题,我提出的问题的唯一解决方案是删除我的代码或找出日期计算出错的原因。
  • get_the_time() 是做什么的?
  • @Qirel get_the_time() 检索撰写帖子的时间(从 WordPress 法典复制/粘贴)。在本例中,get_the_time 获取订单的创建日期。
  • 创建日期字段的名称是什么?

标签: php wordpress date woocommerce


【解决方案1】:

您可以通过运行带有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)");
} 

【讨论】:

  • 这太棒了。非常感谢。解决了问题,也改进了很多代码!
【解决方案2】:

您将这些变量视为 DateTime 实例,但它们是字符串。这个$order_time &lt; $expiration_date 按字母顺序比较字符串,而不是按日期含义。请改用 DateTime 类 (https://www.php.net/manual/en/class.datetime.php)。

【讨论】:

    【解决方案3】:

    请将日期格式从 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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多