第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 從今天開始計算過去的日期

PHP 從今天開始計算過去的日期

PHP
蠱毒傳說 2022-09-03 17:30:14
我創(chuàng)建了一個PHP函數(shù),用于計算WordPress / WooCommerce訂單的年齡。如果訂單超過90天,則應取消。該功能曾經(jīng)完美地工作。然而,自2020年新年以來,它已經(jīng)停止工作。我認為這是因為該函數(shù)對年份感到困惑,因為從今天開始的-90天是2019年。如何使計算與過去年份/2019年一起使用?我嘗試過從WordPress codex而不是mdy中播放不同的日期格式。但是,這似乎沒有任何區(qū)別。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' );
查看完整描述

3 回答

?
慕森王

TA貢獻1777條經(jīng)驗 獲得超3個贊

您可以通過運行帶有子句的查詢來簡化此操作,以僅提取那些超過 90 天的訂單。無需獲取所有結(jié)果并循環(huán)訪問結(jié)果。UPDATEWHERE


您需要將 設(shè)置為列的實際名稱。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)");


查看完整回答
反對 回復 2022-09-03
?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

您將這些變量視為 DateTime 實例,但它們是字符串。這將按字母順序比較字符串,而不是按日期含義進行比較。請改用日期時間類 (https://www.php.net/manual/en/class.datetime.php)。$order_time < $expiration_date

查看完整回答
反對 回復 2022-09-03
?
MMMHUHU

TA貢獻1834條經(jīng)驗 獲得超8個贊

請將日期格式從 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' );


查看完整回答
反對 回復 2022-09-03
  • 3 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號