Даты заказа в PHP - PullRequest
       10

Даты заказа в PHP

0 голосов
/ 06 августа 2020

У меня есть веб-сайт, на котором отображаются заказы клиентов, но мне нужно отображать заказы, которые были заказаны первыми, а не самые новые заказы. Я нашел код в одном из файлов php, но не совсем уверен, что я могу изменить, чтобы это стало возможным. Не могли бы вы мне помочь?

public static function get_bookings( $date_start, $date_end, $args = '', $by = 'booking_date', $limit = '', $offset = '' ,$all = '')  {

    global $wpdb;
    
    // if(strlen($date_start)<10){
    //     if($date_start) { $date_start = $date_start.' 00:00:00'; }
    //     if($date_end) { $date_end = $date_end.' 23:59:59'; }
    // }
    
    // setting dates to MySQL style
    $date_start = esc_sql ( date( "Y-m-d H:i:s", strtotime( $wpdb->esc_like( $date_start ) ) ) );
    $date_end = esc_sql ( date( "Y-m-d H:i:s", strtotime( $wpdb->esc_like( $date_end ) ) ) );
    
    // filter by parameters from args
    $WHERE = '';
    $FILTER_CANCELLED = "AND NOT status='cancelled' ";
    if ( is_array ($args) )
    {
        foreach ( $args as $index => $value ) 
        {

            $index = esc_sql( $index );
            $value = esc_sql( $value );

            if ( $value == 'approved' ){ 
                $WHERE .= " AND ( (`$index` = 'confirmed') OR (`$index` = 'paid') )";
            } else {
              $WHERE .= " AND (`$index` = '$value')";  
            } 
            if( $value == 'cancelled' ){
                $FILTER_CANCELLED = '';
            }
        
        }
    }
    if($all == 'users'){
        $FILTER = "AND NOT comment='owner reservations'";
    } else {
        $FILTER = '';
    }

    if ( $limit != '' ) $limit = " LIMIT " . esc_sql($limit);
    
    if ( is_numeric($offset)) $offset = " OFFSET " . esc_sql($offset);
    
    switch ($by)
    {

        case 'booking_date' :
            $result  = $wpdb -> get_results( "SELECT * FROM `" . $wpdb->prefix . "bookings_calendar` WHERE ((' $date_start' >= `date_start` AND ' $date_start' <= `date_end`) OR ('$date_end' >= `date_start` AND '$date_end' <= `date_end`) OR (`date_start` >= ' $date_start' AND `date_end` <= '$date_end')) $WHERE $FILTER $FILTER_CANCELLED $limit $offset", "ARRAY_A" );
            break;

            
        case 'created_date' :
            // when we searching by created date automaticly we looking where status is not null because we using it for dashboard booking
            $result  = $wpdb -> get_results( "SELECT * FROM `" . $wpdb->prefix . "bookings_calendar` WHERE (' $date_start' <= `created` AND ' $date_end' >= `created`) AND (`status` IS NOT NULL)  $WHERE $FILTER_CANCELLED $limit $offset", "ARRAY_A" );
            break;
        
    }
    
    return $result;

}

/**
* Get maximum number of bookings between dates filtred by arguments, used for pagination
*
* @param  date $date_start in format YYYY-MM-DD
* @param  date $date_end in format YYYY-MM-DD
* @param  array $args fot where [index] - name of column and value of index is value
*
* @return array all records informations between two dates
*/
public static function get_bookings_max( $date_start, $date_end, $args = '', $by = 'booking_date' )  {

    global $wpdb;

    // setting dates to MySQL style
    $date_start = esc_sql ( date( "Y-m-d H:i:s", strtotime( $wpdb->esc_like( $date_start ) ) ) );
    $date_end = esc_sql ( date( "Y-m-d H:i:s", strtotime( $wpdb->esc_like( $date_end ) ) ) );

    // filter by parameters from args
    $WHERE = '';
    $FILTER_CANCELLED = "AND NOT status='cancelled' ";
    if ( is_array ($args) )
    {
        foreach ( $args as $index => $value )
        {

            $index = esc_sql( $index );
            $value = esc_sql( $value );

            if ( $value == 'approved' ){
                $WHERE .= " AND (`$index` = 'confirmed') OR (`$index` = 'paid')";
            } else {
              $WHERE .= " AND (`$index` = '$value')";
            }
            if( $value == 'cancelled' ){
                $FILTER_CANCELLED = '';
            }

        }
    }

    switch ($by)
    {

        case 'booking_date' :
            $result  = $wpdb -> get_results( "SELECT * FROM `" . $wpdb->prefix . "bookings_calendar` WHERE ((' $date_start' >= `date_start` AND ' $date_start' <= `date_end`) OR ('$date_end' >= `date_start` AND '$date_end' <= `date_end`) OR (`date_start` >= ' $date_start' AND `date_end` <= '$date_end')) AND NOT comment='owner reservations' $WHERE $FILTER_CANCELLED", "ARRAY_A" );
            break;


        case 'created_date' :
            // when we searching by created date automaticly we looking where status is not null because we using it for dashboard booking
            $result  = $wpdb -> get_results( "SELECT * FROM `" . $wpdb->prefix . "bookings_calendar` WHERE (' $date_start' <= `created` AND ' $date_end' >= `created`) AND (`status` IS NOT NULL) AND  NOT comment = 'owner reservations' $WHERE $FILTER_CANCELLED", "ARRAY_A" );
            break;

    }

    return $wpdb->num_rows;

}

/**
* Get latest bookings number of bookings between dates filtred by arguments, used for pagination
*
* @param  date $date_start in format YYYY-MM-DD
* @param  date $date_end in format YYYY-MM-DD
* @param  array $args fot where [index] - name of column and value of index is value
*
* @return array all records informations between two dates
*/
public static function get_newest_bookings( $args = '', $limit, $offset = 0 )  {

    global $wpdb;

    // setting dates to MySQL style
   
    // filter by parameters from args
    $WHERE = '';

    if ( is_array ($args) )
    {
        foreach ( $args as $index => $value ) 
        {

            $index = esc_sql( $index );
            $value = esc_sql( $value );

            if ( $value == 'approved' ){ 
                $WHERE .= " AND status IN ('confirmed','paid')";
            } else {
              $WHERE .= " AND (`$index` = '$value')";  
            } 
        
        }
    }
    if ( $limit != '' ) $limit = " LIMIT " . esc_sql($limit);
    //if(isset($args['status']) && $args['status'])
    $offset = " OFFSET " . esc_sql($offset);
   
    // when we searching by created date automaticly we looking where status is not null because we using it for dashboard booking
    $result  = $wpdb -> get_results( "SELECT * FROM `" . $wpdb->prefix . "bookings_calendar` WHERE  NOT comment = 'owner reservations' $WHERE ORDER BY `" . $wpdb->prefix . "bookings_calendar`.`created` DESC $limit $offset", "ARRAY_A" );
     
    
    return $result;

}

1 Ответ

0 голосов
/ 06 августа 2020

Добавьте ORDER BY ... ASC в свой $result = ... sql запрос

для получения дополнительной информации посетите этот сайт - 1. https://www.w3schools.com/sql/sql_ref_order_by.asp

https://www.w3schools.com/sql/sql_orderby.asp
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...