使用mysql使用时区php查找打开和关闭状态

xytpbqjk  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(414)

所有代码都在工作,但我需要时区方面的帮助。。如何在此查询中使用时区?

public function res_openandclose($res_id,$time_open,$time_close)
    {
        $db=getDB();
        $timezone = '<span class="timezone"></span>';
        date_default_timezone_set($timezone);
        $stmt = $db->prepare("SELECT * FROM  fooddelivery_restaurant  WHERE id=:res_id AND CURTIME() >= :open_time and CURTIME() <= :close_time ");
        $stmt->bindParam("res_id", $res_id, PDO::PARAM_STR);
        $stmt->bindParam("open_time", $time_open, PDO::PARAM_STR);
        $stmt->bindParam("close_time", $time_close, PDO::PARAM_STR);
        $stmt->execute();
        $count = $stmt->rowCount();
        if($count)
        {
            return "open";
        }
        else
        {
            return "closed";
        }
    }
4urapxun

4urapxun1#

不能从由javascript动态设置的dom元素中获取值。您将需要使用jquery+ajax+php的组合。
我将做一些假设:
您的php文件名为“process.php”
您的html文档(其中 <span class="timezone"></span> 包含)称为form.php
尽管我不喜欢在form.php中包含jquery/javascript,但为了便于回答这个问题,我还是会包含它。
javascript设置的时区遵循以下常规格式:http://php.net/manual/en/timezones.php
在这种情况下,请尝试以下操作:
第一步:
如果尚未包含,请在form.php的头中包含jquery。您可以将其复制/粘贴到form.php的标题中:

<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>

第二步
在文档正文中,但在 </body> 结束标记:

<script>
$(document).ready(function() {  
        "use strict";
        var TimeZone = ($(".timezone").html());

        $.ajax({
            type: "post",
            dataType: "html",
            url: "process.php",
            data: {Purpose : "CallFunction", Function: "res_openandclose", TimeZone : TimeZone},
            success: function (response) 
                {
                      alert(response); //will alert either "open" or "closed"
                }
        });
    });//end function
</script>

第三步
修改process.php以反映以下内容

//Receive Parameters from AJAX for Function Calls
if(isset($_POST["Purpose"]) && $_POST["Purpose"] === "CallFunction") {

    if(isset($_POST["Function"]) && $_POST["Function"] === "res_openandclose") {
        res_openandclose($res_id,$time_open,$time_close);
    }//end if

}//end if

    public function res_openandclose($res_id,$time_open,$time_close)
        {
if(isset($_POST["TimeZone"]) {
$timezone = $_POST["TimeZone"];
            $db=getDB();
            date_default_timezone_set($timezone);
            $stmt = $db->prepare("SELECT * FROM  fooddelivery_restaurant  WHERE id=:res_id AND CURTIME() >= :open_time and CURTIME() <= :close_time ");
            $stmt->bindParam("res_id", $res_id, PDO::PARAM_STR);
            $stmt->bindParam("open_time", $time_open, PDO::PARAM_STR);
            $stmt->bindParam("close_time", $time_close, PDO::PARAM_STR);
            $stmt->execute();
            $count = $stmt->rowCount();
            if($count)
            {
                return "open";
            }
            else
            {
                return "closed";
            }
        }

保存所有文件并运行脚本。

stszievb

stszievb2#

给你

public function res_openandclose($res_id,$time_open,$time_close, $time_zone)
{
    $db = getDB();

    $stmt = $db->prepare("SELECT * FROM  fooddelivery_restaurant  WHERE id=:res_id AND CURTIME() >= :open_time and CURTIME() <= :close_time and timezone = :time_zone");

    $stmt->bindParam("res_id", $res_id, PDO::PARAM_STR);
    $stmt->bindParam("open_time", $time_open, PDO::PARAM_STR);
    $stmt->bindParam("close_time", $time_close, PDO::PARAM_STR);
    $stmt->bindParam("time_zone", $time_zone, PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();
    if($count){
        return "open";
    }
    else{
        return "closed";
    }
}

相关问题