Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I possible to work with this code to work with date format Y-n-j

Value

start_date = 2011-1-20
end_date   = 2011-1-30

Here are the original function came from this Query between two meta values?

function date_check_join( $join ) {
    global $wpdb;
    $join .= " JOIN ".$wpdb->postmeta." AS startdate ON 
        (".$wpdb->posts.".ID = startdate.post_id AND
        startdate.meta_key = 'start_date')
        JOIN ".$wpdb->postmeta." AS enddate ON 
        (".$wpdb->posts.".ID = enddate.post_id AND
        enddate.meta_key = 'end_date')";
    return $join;
    }

function date_check_where( $where ) {
    $today = date('Y-n-j'); /* I change this format to date('Y-n-j')*/
    $where .= " AND startdate.meta_value <= $today
    AND enddate.meta_value >= $today";
    return $where;
    }

This function is working with UNIX TIME STAMP date('Ymd') and when I change date format it doesn't work with date('Y-n-j') credit @goldenapples

share|improve this question
    
have you tried strtotime ? –  Cronco Jan 27 '11 at 4:20
    
@Crono Yes but nothing happen it doesn't return any data –  idontknowhow Jan 27 '11 at 7:40
add comment

2 Answers

Hi @idontknowhow:

You've focused on trying to solve the problem in PHP when in fact you needed to focus on the SQL. Your first problem was you reversed the "polarity" (which is a term I made up just now, but it kinda fits); you were looking for both a start date that was greater than today and an end date the was earlier than today; a logical impossibility.

Second problem ignoring the reversed polarity was more subtle; you were comparing dates with equal length strings so you could get a false positive. This SQL illustrates the problem:

SELECT * FROM wp_posts WHERE '2011-9-20' < '2011-11-20'

The WHERE criteria "looks" true and thus the query should return all records when in fact it returns none and that is because '2011-9' < '2011-1' is actually false.

So here's a working replacement for your date_check_where() function that uses the SQL DATE() function to normalize the strings in dates into real date values so they can be compared and it of course fixes the polarity issue I mentioned at first:

add_filter( 'posts_where', 'date_check_where' );
function date_check_where( $where ) {
  $today = date('Y-m-d');
  $where .=<<<SQL
 AND DATE(startdate.meta_value) >= DATE('{$today}')
 AND DATE(enddate.meta_value) <= DATE('{$today}')
SQL;
  return $where;
}

Note I used date('Y-m-d') instead of date('Y-n-j') because I find it cleaner to use, and it doesn't need to make the meta fields because of the DATE() function.

share|improve this answer
add comment

Try to put values for comparison in quotes, those minuses might be getting it interpreted as mathematical expression instead of string:

$where .= " AND 'startdate.meta_value' <= '$today'
    AND 'enddate.meta_value' >= '$today'";
share|improve this answer
    
thanks but it doesn't work is there any solution for this.. .thanks –  idontknowhow Jan 27 '11 at 23:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.