Home > OS >  My IF condition is not working and is directly jumping into the ELSE statement
My IF condition is not working and is directly jumping into the ELSE statement

Time:10-17

I want to check whether the current time is past 12p.m and pass and execute the codes indie the IF statement but it is directly going into the Else statement. I think the condition is not right. May I know the right way of finding out whether the current time has past the specific time that I've given?

My codes

<?php

                if (date('H') > 12){

                    $myDate = date('m/d/Y');

                    include_once '../classes/config.php';

                    $con = mysqli_connect(HOST, USER, PASS, DB) or die('Connection Error! '.mysqli_error($con));

                    $checkuser = mysqli_query($con, "SELECT * FROM patient_diary WHERE patient_id = '$id' AND date = '$myDate'") or die(mysqli_error($con));
                    $result = mysqli_num_rows($checkuser);
                   
                    if ($result){?>
                        <div class="head">
                            <div class="title">INSERT STATUS</div>
                        </div>
                        <form action="" method="POST">
                            <div class="insert-details">
                                <div class="input-value">
                                    <small>Condition</small>
                                    <select name="condition" class="conditionscale" required>
                                        <option value="" selected hidden>Select status</option>
                                        <option value="much better">Much Better</option>
                                        <option value="better">Better</option>
                                        <option value="same">Same</option>
                                        <option value="worse">Worse</option>
                                        <option value="much worse">Much Worse</option>
                                    </select>
                                </div>
                                <div class="input-value">
                                    <small>Food</small>
                                    <textarea type="text" name="role" placeholder="What did you have?" required></textarea>
                                </div>
                                <div class="input-value">
                                    <small>Date</small>
                                    <input type="text" id="date" name="date" placeholder="MM-DD-YYYY" onfocus="(this.type='date')" onblur="(this.type='text')" required>
                                </div>
                            </div>
                            <div class="insert-elements">
                                <input id="reset" type="reset" name="reset"  class="reset-btn" value="Reset">
                                <input type="submit" name="submitInsert" class="insert-btn" value="Enter" id="insertbtn">
                            </div>
                    <?php } else{ ?>
                        <div class="no-head">
                            <div class="title">You can enter your status details only once a day.</div>
                        </div>
                    <?php } 
                } else{ ?>
                    <div class="no-head">
                        <div class="title">You are allowed to enter your status everyday after 6 p.m only</div>
                    </div>
                <?php
                }
            ?>

CodePudding user response:

If you use "strtotime" when comparing dates, the problem goes away.

CodePudding user response:

My timezone was not properly set inside the php.ini file. But even without making any changes over there I used the below code to get the right current hour I'm in by declaring my region and city.

  date_default_timezone_set('Region/City');

You can also refer to this link of lists to find what you exactly have to type to get the right time for the place you want.

  • Related