douyi02577 2018-03-25 22:31
浏览 100
已采纳

如何确定给定的开始和结束时间是否已在php中过期

Please i want to create a php code to check if time has expired or still active based on the start date and end time that users specified. Below is the database structure and sample php code which i tried.

game_config

game_id | start_date | start_t | start_h | end_time | end_h | status  
--------|------------|---------|---------|----------|-------|---------
100     | 03/26/2018 |  10:45  | PM      |   12:30  |  AM   | 0 
101     | 03/27/2018 |  09:23  | AM      |   11:10  |  AM   | 0 

Php code sample

<?php
    $conf_execution_date = date('m/d/Y'); /*Current date*/
    $conf_execution_meridiem = date('A'); /*Current meridiem*/
    $conf_execution_timer = date('h:m'); /*Current time*/
    $conf_execution_proccess = false;    /*Proceed or not*/

    $conf_handler = $appconn->prepare("
        SELECT * FROM game_config WHERE game_id = :game_id AND start_date = :start_dateAND start_t = :start_t AND status = 0 LIMIT 1
    ");

    $conf_handler->bindParam(":game_id", 100);
    $conf_handler->bindParam(":start_date", $conf_execution_date);
    $conf_handler->bindParam(":start_t", $conf_execution_meridiem); 
    $conf_handler->execute();
    $appconf = $conf_handler->fetch(PDO::FETCH_OBJ);

    if($appconf){
        $config_start_time = strtotime($appconf->start_t);
        $config_end_time = strtotime($appconf->end_time);
        $current_time = strtotime($conf_execution_timer);

        if($appconf->start_h == $conf_execution_meridiem){ /*AM-PM*/
            if($config_start_time >= $current_time){ /*Check if the start time is now o still active*/
                $conf_execution_proccess = true;
            }
        }

        if($appconf->end_h == $conf_execution_meridiem){ /*AM-PM*/
            if($config_end_time >= $current_time){ /*Check if the time has ended*/
                $conf_execution_proccess = false;
            }
        }

        if($conf_execution_proccess == true){
            echo 'YES THE GAME IS STILL ACTIVE';
        }else{
            echo "NO GAME HAS ENDED";
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • douhuantui6259 2018-03-26 00:27
    关注

    Assuming your start_date column is type VARCHAR():

    Assuming your end_time is always later in the day than your start_time:

    You can use this sort of query to get a starting and ending DATETIME value for each row of your table. It glues together the date, the time, and the AM/PM fields and converts them to DATETIME. (http://sqlfiddle.com/#!9/2d84ea/1/0)

    SELECT game_id, 
           STR_TO_DATE(CONCAT(start_date,'-',start_t,start_h), '%m/%d/%Y-%h:%i%p') start_ts,
           STR_TO_DATE(CONCAT(start_date,'-',end_time,end_h), '%m/%d/%Y-%h:%i%p') end_ts,
           status    
      FROM game_config
    

    Then you can compare those start_ts and end_ts values to NOW() by appending this line to your query

    Then you can fill out your query with the criteria you need, giving this query.

    SELECT game_id, 
           STR_TO_DATE(CONCAT(start_date,'-',start_t,start_h), '%m/%d/%Y-%h:%i%p') start_ts,
           STR_TO_DATE(CONCAT(start_date,'-',end_time,end_h), '%m/%d/%Y-%h:%i%p') end_ts,
           status    
      FROM game_config
     WHERE game_id = : gameid AND status = 0
    HAVING start_ts <= NOW() and NOW() < end_ts
     LIMIT 1
    

    But, your start and end times sometimes span midnight, which means your end_ts needs to be on the next day. That makes your date-checking logic quite a bit trickier. You'll have to do some sort of conditional expression in which you do + INTERVAL 1 DAY to your end_ts if it is before your start_ts.

    You will be much better off (@eggyal pointed this out in his comment) if you put the following columns in your table.

    game_id   int
    start_ts  timestamp of start
    end_ts    timestamp of end
    status    int
    

    Then you don't have to worry about all the stuff about making a proper timestamp from three different columns, and you don't have to do anything special when the start and end times span midnight.

    And, this will work intuitively no matter what timezone your users are in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流