0

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
                }
            ?>
  • Print the result of `date('H')` to see what it returns, maybe it does not contain what You expect. It uses timezone configured on server. – Roman Hocke Oct 17 '21 at 08:07
  • 1
    Yah I checked it. As you said it doesn't show the right time. How to correct it? – learning Gatherer Oct 17 '21 at 08:10
  • 1
    What's the real question then? Do you want to calculate noon in a given time zone? – Álvaro González Oct 17 '21 at 08:53
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 17 '21 at 12:13
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 17 '21 at 12:13

2 Answers2

1

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.

0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Tyler2P Oct 17 '21 at 08:09
  • I tried to print 'strtotime('H')' and it shows '1634436938' as the output. How to get the exact hour number that I want? – learning Gatherer Oct 17 '21 at 08:16