Jump to content

Show database entries by todays date plus 7 days


Go to solution Solved by EarthDay,

Recommended Posts

Hi there,

I am trying to show entries in my database that have todays date plus seven days.

My code is currently 

$stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc');

This does not work sadly.

Any thoughts?

Cheers, ED.

Edited by EarthDay
9 minutes ago, Barand said:

Try

$stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date > CURDATE() + INTERVAL 7 DAY");

Hi Barand,

Thank you for this.

I have updated my code and get the following error message;

Quote

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /reminders.php:6 Stack trace: #0 /reminders.php(6): PDOStatement->execute() #1 {main} thrown in /reminders.php on line 6

Any ideas?

Cheers, ED.

The full code is 

$now = date("Y-m-d h:i:sa");
$stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc');
$stmt->execute([$now]);
$allReview = $stmt->fetchAll(PDO::FETCH_ASSOC);

$now = date("Y-m-d h:i:sa");
$stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc');
$stmt->execute([$now]);
$allReview = $stmt->fetchAll(PDO::FETCH_ASSOC);

 

My query didn't involve any external variables as the date+7 is calculated in the query. I therefore used query() instead of prepare() - there is no execute() required with query().

Incidentally, your code just compares against now, now 7 days from now,

  • Solution

Hi All,

Thank you all for your help on this :)

I have figured this out by using the below code

$stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date between now() and now() + INTERVAL 7 DAY");

Cheers,

ED.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.