简体   繁体   English

我如何检查用户无法在php中输入超过8小时的时间

[英]How i can Check That a user cannot enter time hours more than 8 hours in php

My code is taking for 8 hours from time and date field but when i give both for example i set 9 am to 4 pm then its OK. 我的代码从时间和日期字段开始需要花费8个小时,但是当我同时给出两者时,我将上午9点设置为下午4点,那么就可以了。 but when i insert 9am to 6am it also allow that one its almost 21 hours difference so how can i do it any one please? 但是,当我将上午9点插入到上午6点时,它也允许相差将近21个小时,所以我该怎么做?

<?php

elseif (isset($_POST['from']) && ($_POST['to']))
{
    $from_time = $_POST['from'];
    $to_time = $_POST['to'];
    $starttimestamp = strtotime($from_time);
    $endtimestamp = strtotime($to_time);
    $difference = abs($endtimestamp - $starttimestamp) / 3600;

    if ($difference > 8){
        echo "<script>alert('Oops!Your Daily Availablity Is More Than 8 Hours')</script>";
    }else{
        $query = "update donor SET from_time='" . $from_time . "',to_time='" . $to_time . "' where id=" . $_SESSION['id'];
    }

Take the input in 24 hrs format including DataTime. 以24小时格式输入,包括DataTime。 Then change your code as below. 然后如下更改您的代码。

I hope this code will solve your problem. 我希望这段代码能解决您的问题。

<?php
        date_default_timezone_set('Asia/Kathmandu'); 

        $datetime1 = new DateTime('2010-10-11 24:00:00'); // change date & time
        $datetime2 = new DateTime('2010-11-11 24:00:00');  //change date & time
        $interval = $datetime1->diff($datetime2);
        echo $interval->format('%Y-%m-%d %H:%i:%s')."<br />";

        $year =  $interval->format('%Y') * 365 * 24;
        $month = $interval->format('%m') * 30 * 24 ;
        $day = $interval->format('%d') * 24 ;
        $hour = $interval->format('%H');
        $minute = $interval->format('%i')/ 60;
        $seconds = $interval->format('%s')/(60*60);

        $time_diff = $year + $month + $day + $hour + $minute + $seconds;

        echo $time_diff;

        if ($time_diff > 8) {
            echo 'More then 8 hour';
        }else{
            echo 'ok';
        }
?>

you have to specify full date time in request. 您必须在请求中指定完整日期时间。

  <?php
      $date = date("Y-m-d h:i:s", strtotime($row['time_d']))
  ?>

  <input type="time" class="form-control" values="<?= $date ?>" id="until_t" name="from"/>

Then you will got correct difference between both start and end time. 然后,您将在开始时间和结束时间之间获得正确的时差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM