【问题标题】:Find open and closed status using mysql useing timezone php使用 mysql 使用时区 php 查找打开和关闭状态
【发布时间】:2018-05-11 09:52:14
【问题描述】:

所有代码都在工作,但我需要时区的帮助.. 如何在这个查询中使用时区?

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";
        }
    }

【问题讨论】:

  • 这个问题有点笼统。请指定如何您想使用什么时区(用户的?服务器的?餐厅的?全部的?)。
  • 我们需要一些额外的信息来帮助您, 来自哪里?是动态设置的吗?如果是这样,您将需要使用 jQuery+AJAX 将值解析为 PHP。
  • @jeff 这个用户时间
  • @JulianKoster 来自 java 脚本用户当前时区

标签: php mysql sql timezone


【解决方案1】:

您不能从由 Javascript 动态设置的 DOM 元素中获取值。您将需要使用 jQuery + AJAX + PHP 的组合。

我会做一些假设:

  1. 您的 PHP 文件名为“process.php”
  2. 您的 HTML 文档(其中包含 &lt;span class="timezone"&gt;&lt;/span&gt;)称为 form.php
  3. 尽管我不喜欢在 form.php 中包含 jQuery/Javascript,但为了方便回答,我还是会包含它。
  4. 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>

第二步

在文档的正文中包含以下内容,但就在 &lt;/body&gt; 结束标记之前:

<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";
            }
        }

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

【讨论】:

    【解决方案2】:

    这里是

    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";
        }
    }
    

    【讨论】:

    • 我不想要静态时区
    • 在函数参数中传递 $time_zone 像这样 res_openandclose($res_id,$time_open,$time_close, $time_zone) 而不是在函数中定义它
    • 立即查看。添加 $time_zone 作为函数的参数
    猜你喜欢
    • 1970-01-01
    • 2014-07-17
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多