0

I think there's something off with my query. I can't get the right function that I want.

Example scenario:

If I recorded a prenatal checkup today, the add button will not appear after the next 30 days or one month.

But with this query after adding checkup today, the next day, the add button appeared already.

Here's my code:

$dateToday = date('Y-m-d');
$checkPatients = $conn->query("SELECT last_checkup FROM `prenatal_checkup` WHERE  itr_no = '$_GET[id]' limit 1");
$fetchPatients = $checkPatients->fetch_array();
if($fetchPatients['last_checkup'] >= (date('m', strtotime('+1 month')) . '/01/' . date('Y', strtotime('+1 month')))){
    echo '<div class="alert alert-danger" role="alert" style="font-weight: bold; margin-bottom:-13px;"><p>
        '.ucfirst($_GET['firstname'])." ".ucfirst($_GET['lastname']). ' was already estimated her date of confinement.
        Adding new data will be available again after one month. Please click the record below to proceed to next check up.</p></div>';
}
else
{
    ?>
    <button class = "btn btn-primary" id = "show_com"><i class = "glyphicon glyphicon-plus">ADD</i></button>
    <?php
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 2
    Note, your query is not secure because you are directly feeding it user-supplied data (`$_GET`). Please implement a mysqli prepared statement with placeholders. It appears you are making a date string comparison but `m-d-Y` might not reliably compare as a string. – mickmackusa Feb 23 '18 at 00:23
  • What is in `$fetchPatients['edc']` – RiggsFolly Feb 23 '18 at 00:25
  • @RiggsFolly it's the estimated date of confinement of the patient – TechieMickey Feb 23 '18 at 00:27
  • I'm not completely sure I like where you got this from: https://stackoverflow.com/questions/13422032/get-1st-day-of-next-month But I do like that you did research before asking. This looks more reliable considering year-rollover: https://3v4l.org/gMJQu – mickmackusa Feb 23 '18 at 00:29
  • @TechieMickey I think RiggsFolly is asking you to confirm that your `edc` value is also in `m-d-Y` format. If not, we need to know what format it is in. – mickmackusa Feb 23 '18 at 00:46
  • So you realize that in m/d/y format Nov/01/2018 < Dec/01/1993, right? Use y/m/d. Or use Datetime objects instead. – Sammitch Feb 23 '18 at 01:13
  • @TechieMickey Please also take the time to improve your question pertaining to the requested date format. – mickmackusa Feb 24 '18 at 07:52

1 Answers1

0

I've found the solution for my problem. I just change the

if($fetchPatients['last_checkup'] >= (date('m', strtotime('+1 month')) . '/01/' . date('Y', strtotime('+1 month')))){ 

to

if (strtotime($fetchPatients['last_checkup']) < strtotime('-30 days'))`

and it works fine.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Please only use quote formatting for actual quoted information, it should not be used to highlight portions of a post. Please award your own answer the green tick so that this page is deemed resolved by the system. – mickmackusa Feb 24 '18 at 08:24