1

Apologies for the exceptionally vague title, I'm not sure of the exact terminology to use here.

I've written some PHP to store a variable for the current year and then another variable to store the current year minus 4 (four years ago). As a note I do want it to be the whole year, hence appending 1 Jan.

// Get current date
$date = date_create('now');

// Store two digit version of current year
$yearnow = date_format($date, 'y') . "-01-01";

// Reduce the year count by two
$yearminusfour = $yearnow - 4 . "-01-01";

No problems there, although maybe there's a more efficient way to do this. However, I'm then using this to dynamically grab posts from the last four years in WordPress.

This line of code that's responsible for returning the right posts works fine when static:

$where .= " AND post_date >= '2010-01-01' AND post_date < '2014-01-01'";

What I'm struggling with is combining the two to make it dynamic. I've tried:

$where .= " AND post_date >= $yearnow AND post_date < $yearminusfour";
$where .= " AND post_date >= '.$yearnow.' AND post_date < '.$yearminusfour.'";
$where .= " AND post_date >= '".$twentyten."' AND post_date < '".$yearminusfour."'";

I'm stuck and I'm sure I'm being slow.

3
  • 1
    You put concatenation dots there: '. but didn't stopped the string with: " (e.g. '".) Commented Jan 7, 2015 at 15:23
  • The 3rd is good syntactically (the $twenteen should be $yearnow isn't it?). The first 2 one is bad. Dates need to wrapped by single quote, in 2nd the dots . are unnecessary. Commented Jan 7, 2015 at 15:24
  • 1
    Or you know, you could just read up on PDO and parameter binding. Commented Jan 7, 2015 at 15:25

7 Answers 7

1
$where .= " AND post_date >= '$yearnow' AND post_date < '$yearminusfour'";

That is the correct version. You don't need those periods in there.

But, as you know yourself. That's an unnecessarily complicated computation of dates which results in incorrect values. You can for example get first date of this year by using

echo date("Y-m-d",strtotime("1st January this year"));  // 2015-01-01

How can I prevent SQL injection in PHP?

Sign up to request clarification or add additional context in comments.

Comments

1
$where .= " AND post_date >= '".$twentyten."' AND post_date < '".$yearminusfour."'";

This will work but you have to reverse comparisons :)

$where .= " AND post_date <= '".$twentyten."' AND post_date >= '".$yearminusfour."'";

Also, you should use prepared statements. It's recommended when you are using user input.

2 Comments

Revert: Change back to a previous version. Reverse: Switch order. I think reverse is correct since OP didn't have a different order to begin with :P
Oh, lesson for me: don't trust google translate, always check in dictionary.. ;)
0

I think the range you are searching for is not feasable. According to the code you have posted, it searches for posts >= the current year and less than the date 4 years ago. So you need to change the comparisons.

$where .= " AND post_date <= '".$twentyten."' AND post_date > '".$yearminusfour."'";

Comments

0

$yearnow is something like 2015-01-01. Then what would $yearnow-4 be?
Anyway, you can use the DateTime class (of which you already used the alias date_create) to perform the necessary manipulations:

<?php
$date = new DateTime('first day of January');


$clause = sprintf('WHERE x BETWEEN %s AND %s',
    $date->format('Y-m-d'),
    $date->modify('- 4 year')->format('Y-m-d')
);

echo $clause;

prints (this year)

WHERE x BETWEEN 2015-01-01 AND 2011-01-01

3 Comments

why not SQL functions like DATE_SUB
Ah, you're absolutely right, I changed some of my code around and now it doesn't make any sense. I previously had each variable as just the year before appending -01-01 and then added the day and month on later. Tried to simplify but obviously messed up! I'll give you're answer a go.
Royal Bg, because wordpress doesn't have to be used with MySQL, and the impact isn't big enough for me to deviate even more from the actual question ;-)
0

How about?

$yearStart = date('Y-m-d', strtotime('Jan 01'));
$yearEnd = date('Y-m-d',strtotime("$yearStart -4 year")); 

$where .=  "AND (date_field BETWEEN '".$yearStart."' AND '".$yearEnd."')";

1 Comment

This doesn't seem to be working for me although I'm now wondering if my issue runs deeper? However, my original code works fine if I use static dates ie: $where .= " AND post_date >= '2010-01-01' AND post_date < '2014-01-01'";
0

1) To get your dates try:

$currentdate = date('Y-m-d',time());
$yearminus4 = date('Y-m-d',strtotime("-4 years", time()));

2) I think you have your select criteria backwards in your sql statement

$where = " AND post_date >= '" . $yearminus4 . "'  
           AND post_date <= '" . $currentdate . "'";

Comments

0

I'd recommend to use BETWEEN condition of mysql (I assume you use mysql) and date subtraction.

That will look like this:

$where .= " AND post_date BETWEEN '$yearnow' AND '$yearnow' - INTERVAL 4 YEAR";

1 Comment

WordPress+MySQL is almost a given ...almost ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.