PHP 从今天开始计算过去的日期

我创建了一个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' );


蛊毒传说
浏览 118回答 3
3回答

慕森王

您可以通过运行带有子句的查询来简化此操作,以仅提取那些超过 90 天的订单。无需获取所有结果并循环访问结果。UPDATEWHERE您需要将 设置为列的实际名称。post_createdfunction expire_after_x_days() {&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; $result = $wpdb->query("UPDATE $wpdb->posts&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SET post_status = 'wc-cancelled'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE post_type = 'shop_order'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND post_status = 'wc-processing'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND post_created < DATE_SUB(NOW(), INTERVAL 90 DAY)");}&nbsp;

缥缈止盈

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

MMMHUHU

请将日期格式从 m/d/y 更改为 Y-m-d。请参阅以下代码。您也可以通过修改$order_time = '12/11/18'来手动检查;function expire_after_x_days(){&nbsp; &nbsp; &nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; &nbsp; &nbsp; // Get current time&nbsp; &nbsp; &nbsp; &nbsp; $today = date("Y-m-d");&nbsp; &nbsp; &nbsp; &nbsp; // set time to expire&nbsp; &nbsp; &nbsp; &nbsp; $time_to_expire = "-90 days";&nbsp; &nbsp; &nbsp; &nbsp; $expiration_date = date("Y-m-d", strtotime( $today . $time_to_expire));&nbsp; &nbsp; &nbsp; &nbsp; // Get orders with processing status&nbsp; &nbsp; &nbsp; &nbsp; $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");&nbsp; &nbsp; &nbsp; &nbsp; if( !empty($result)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($result as $order){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get order's time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $order_time = get_the_time('Y-m-d', $order->ID );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Compare order's time with current time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //$order_time = '12/11/18';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $order_time < $expiration_date ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //die("olde");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update order status&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $orders = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $orders['ID'] = $order->ID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $orders['post_status'] = 'wc-cancelled';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_update_post( $orders );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo 'not old date';die;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp;add_action( 'admin_footer', 'expire_after_x_days' );
打开App,查看更多内容
随时随地看视频慕课网APP