使用mysql使用时区php查找打开和关闭状态
所有代码都正常,但我需要时区方面的帮助。。 如何在此查询中使用时区使用mysql使用时区php查找打开和关闭状态,php,mysql,sql,timezone,Php,Mysql,Sql,Timezone,所有代码都正常,但我需要时区方面的帮助。。 如何在此查询中使用时区 public function res_openandclose($res_id,$time_open,$time_close) { $db=getDB(); $timezone = '<span class="timezone"></span>'; date_default_timezone_set($timezone); $stm
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";
}
}
公共函数res\u open和close($res\u id,$time\u open,$time\u close)
{
$db=getDB();
$timezone='';
日期\默认\时区\设置($timezone);
$stmt=$db->prepare(“SELECT*FROM fooddelivery\u restaurant,其中id=:res\u id和CURTIME()>=:open\u time和CURTIME()”您不能从由Javascript动态设置的DOM元素中获取值。您需要使用jQuery+AJAX+PHP的组合
我将做一些假设:
您的PHP文件名为“process.PHP”
您的HTML文档(其中包含
)称为form.php
尽管我不喜欢在form.php中包含jQuery/Javascript,但为了便于回答这个问题,我还是会包含它
Javascript设置的时区遵循以下常规格式:
在这种情况下,请尝试以下操作:
第一步:
如果尚未包含,请将jQuery包含在form.php的标题中。您可以将其复制/粘贴到form.php的标题中:
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
第二步
在文档正文中,但就在
结束标记之前,包括以下内容:
<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>
$(文档).ready(函数(){
“严格使用”;
var TimeZone=($(“.TimeZone”).html();
$.ajax({
类型:“post”,
数据类型:“html”,
url:“process.php”,
数据:{用途:“调用函数”,函数:“res_openandclose”,时区:TimeZone},
成功:功能(响应)
{
警报(响应);//将发出“打开”或“关闭”警报
}
});
});//结束函数
第三步
修改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";
}
}
//从AJAX接收函数调用的参数
如果(isset($\u POST[“目的”])&&&$\u POST[“目的”]=“调用函数”){
如果(isset($\u POST[“函数”])&&&$\u POST[“函数”]===“res\u openandclose”){
res_open和close($res_id、$time_open、$time_close);
}//如果结束
}//如果结束
公共函数res\u open和close($res\u id、$time\u open、$time\u close)
{
如果(isset($_POST[“时区”]){
$timezone=$_POST[“timezone”];
$db=getDB();
日期\默认\时区\设置($timezone);
$stmt=$db->prepare(“SELECT*FROM fooddelivery\u restaurant,其中id=:res\u id和CURTIME()>=:open\u time和CURTIME()”您不能从由Javascript动态设置的DOM元素中获取值。您需要使用jQuery+AJAX+PHP的组合
我将做一些假设:
您的PHP文件名为“process.PHP”
您的HTML文档(其中包含
)称为form.php
尽管我不喜欢在form.php中包含jQuery/Javascript,但为了便于回答这个问题,我还是会包含它
Javascript设置的时区遵循以下常规格式:
在这种情况下,请尝试以下操作:
第一步:
如果尚未包含,请将jQuery包含在form.php的标题中。您可以将其复制/粘贴到form.php的标题中:
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
第二步
在文档正文中,但就在
结束标记之前,包括以下内容:
<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>
$(文档).ready(函数(){
“严格使用”;
var TimeZone=($(“.TimeZone”).html();
$.ajax({
类型:“post”,
数据类型:“html”,
url:“process.php”,
数据:{用途:“调用函数”,函数:“res_openandclose”,时区:TimeZone},
成功:功能(响应)
{
警报(响应);//将发出“打开”或“关闭”警报
}
});
});//结束函数
第三步
修改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";
}
}
//从AJAX接收函数调用的参数
如果(isset($\u POST[“目的”])&&&$\u POST[“目的”]=“调用函数”){
如果(isset($\u POST[“函数”])&&&$\u POST[“函数”]===“res\u openandclose”){
res_open和close($res_id、$time_open、$time_close);
}//如果结束
}//如果结束
公共函数res\u open和close($res\u id、$time\u open、$time\u close)
{
如果(isset($_POST[“时区”]){
$timezone=$_POST[“timezone”];
$db=getDB();
日期\默认\时区\设置($timezone);
$stmt=$db->prepare(“从fooddelivery\u餐厅中选择*,其中id=:res\u id和CURTIME()>=:open\u time和CURTIME()在这里
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";
}
}
公共函数res\u open和close($res\u id、$time\u open、$time\u close、$time\u zone)
{
$db=getDB();
$stmt=$db->prepare(“从fooddelivery\u餐厅中选择*,其中id=:res\u id和CURTIME()>=:open\u time和CURTIME()在这里
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";
}
}
公共函数res\u open和close($res\u id、$time\u open、$time\u close、$time\u zone)
{
$db=getDB();
$stmt=$db->prepare(“选择*FROM fooddelivery\u restaurant,其中id=:res\u id AND CURTIME()>=:open\u time AND CURTIME()”)这个问题有点宽泛。请指定如何使用时区(用户的?服务器的?餐厅的?全部?)。我们需要一些额外的信息来帮助您,这些信息来自何处?它是动态设置的吗?如果是这样,您需要使用jQuery+AJAX将值解析为PHP。@jeff此用户Time@JulianKoster来自java脚本用户当前时区此问题有点广泛。请指定如何使用时区(用户的?服务器的?餐厅的?一起?)。我们需要一些额外的信息来帮助您,这些信息来自哪里?它是动态设置的吗?如果是这样,您需要使用jQuery+AJAX将值解析为PHP。@jeff此用户Time@JulianKoster来自java脚本用户当前时区我不希望静态时区通过