【发布时间】:2016-10-13 12:49:05
【问题描述】:
在我的数据库中,我在其中一个表中有一个日期和时间字段,它可以正确显示数据库中我想要的行的日期和时间。由于我可能会获得来自许多不同国家/地区的用户,因此我需要允许用户选择他们的时区,以便在我网站的所有页面中将 dateandtime 字段中的时间和日期中的时间更改为他们的时区。
我能够成功地允许用户在输入字段中输入时区,例如“+3:00”,并在另一个输入字段中输入日期,例如“24/07/2017”,并且当用户单击搜索按钮时区转换将成功。这并不理想,因为这意味着每次用户转到网站上的另一个页面并返回按日期搜索页面时,他们都必须输入时区,例如再次“+3:00”。
我在想可以使用会话来实现我所需要的,但是当我尝试使用会话来记住时区值时,当我返回页面时,时间将不再是我之前输入的时区。
总而言之,我想要实现的是,我希望用户能够选择他们的时区,并且将数据库中网页上显示的任何日期和时间更改为用户选择的时区,如果如果用户要离开页面,那么如果他们要返回上一页,就会记住他们的时区。
下面是一个显示我想要成功实现的示例网站
A site which has the functionality with timezones I am attempting to achieve
正如您在该网站上看到的,如果我选择时区,它会成功更改时区,如果我要选择网站的另一个部分,它会记住用户时区并更新日期和时间以反映时区更改。经过检查,该网站似乎使用 cookie 来记住时区值,我是否可以使用 cookie 在我的网站上实现此类功能?
以下是当用户输入时区值时成功更改时区的代码,例如将“+3:00”和日期输入到相应的输入字段中,但不会记住时区选项。
<?php
include './config.php';
include './header.php';
$timezone = trim($_GET["timezone"]);
$keyword = trim($_GET["keyword"]);
if ($keyword <> "" && $timezone <> "" ) {
$sql = "SELECT f.hometeam, f.awayteam, f.sport, f.competition,
DATE_FORMAT(CONVERT_TZ(f.dateandtime, '+00:00','$timezone'), '%M %e, %Y %r') AS dateandtime,
Group_concat(s.name SEPARATOR ',') name,
Group_concat(x.channelid_fc SEPARATOR ',') channelid_fc
FROM footballfixtures f
LEFT JOIN fixturechannels x
ON x.matchid_fc=f.matchid
LEFT JOIN satellite s
ON x.channelid_fc=s.channelid
WHERE DATE(dateandtime) = STR_TO_DATE('$keyword', '%d/%m/%Y')
GROUP BY f.hometeam, f.awayteam, f.sport, f.competition, f.dateandtime
ORDER BY f.dateandtime ";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":keyword", $keyword."%");
$stmt->bindValue(":timezone", $timezone." %");
$stmt->execute();
} else {
$sql = "SELECT 1 from dual";
$stmt = $DB->prepare($sql);
}
$stmt->execute();
?>
<html>
<head>
</head>
<body>
<div class="container mainbody">
<div class="mainpagetitle">
<h11>Sports Schedule</h11> <br> <br>
<p>We aim to provide you with sports schedule in an easy to view format</p> <br> <br> <br> <br> <br>
<form class="form-inline">
</div>
<div class="clearfix"></div>
<div class="col-xs-12">
<img src="css/calendar.png" class="img-responsive" />
<div id=class="container-fluid">
<div class="row">
<h2>Search by Date</h2> <br>
<p> Please Enter Date </p>
</div>
</div>
</div>
<div class="searchform">
<form action="bydate.php" method="get" >
<label class="col-xs-12" for="timezone";>
<select name="timezone" id="timezone">
<option value="+1:00">+1:00</option>
<option value="+2:00">+2:00</option>
<option value="+3:00">+3:00</option>
<option value="+4:00">+4:00</option>
</select>
</label>
<div class="searchform">
<form action="bydate.php" method="get" >
<label class="col-xs-12" for="keyword";>
<input type="text" value="<?php echo htmlspecialchars($_GET["keyword"]); ?>" placeholder="Enter Team Name" id="" class="form-control" name="keyword">
</label>
<button class="btn btn-info">search</button>
</form>
</div>
</div>
<div class="clearfix"></div>
<div class="container">
<div class="row">
<div class="tables">
<div class="col-xs-12">
<table class="table table-hover footable">
<thead>
<tr>
<th>Home Team</th>
<th> vs </th>
<th>Away Team</th>
<th data-hide="phone, tablet">Sport</th>
<th data-hide="phone, tablet">Competition</th>
<th data-hide="phone, tablet">Date and Time</th>
<th data-hide="phone, tablet">Channels</th>
</tr>
</thead>
<?php
if($stmt->rowCount() >0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$hometeam = $row['hometeam'];
$versus= $row['versus'];
$awayteam= $row['awayteam'];
$sport= $row['sport'];
$competition = $row['competition'];
$dateandtime=$row['dateandtime'];
$name=explode(',', $row['name']);
$channelid=explode(',', $row['channelid_fc']);
?>
<tbody>
<tr>
<td> <?php echo $row[hometeam] ; ?> </td>
<td> <?php echo $row[versus] ; ?> </td>
<td> <?php echo $row[awayteam] ; ?> </td>
<td> <?php echo $row[sport] ; ?> </td>
<td> <?php echo $row[competition] ; ?> </td>
<td> <?php echo $row[dateandtime] ; ?> </td>
<td>
<?php for ($i = 0; $i < count($channelid) && $i < count($name); ++$i) {
$achannelid = $channelid[$i];
$aname = $name[$i];
echo "<a href='http://sportschedule.xyz/view_channels.php?channelid=" .$achannelid."'> ".$aname." </br> </a> ";
}
?>
</tbody>
</td>
</tr>
<?php } ?>
<?php } else { ?>
<p>No matches found for the team you searched. Please try again</p>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
<script>
$('.footable').footable({ addRowToggle: false });
$('.footable').footable({
calculateWidthOverride: function() {
return { width: $(window).width() };
}
});
$(document).ready(function(){ if ($.trim($(".footable tbody").text()).length == 0) { $('.footable').hide(); } });
</script>
</body>
</html>
非常感谢任何阅读和/或建议我如何实现解决方案或引导我朝着正确方向寻求帮助的人
【问题讨论】:
-
我认为您可以使用 javascript 自动执行此操作。 stackoverflow.com/questions/1091372/… 您可以获得浏览器时区,它基本上是用户计算机的时区,您可以使用它。另一个有用的库:momentjs.com/timezone
-
我猜你在每次页面加载时都会调用 session_start() 。如果您在每个页面中都包含这些内容,则可以将其放在您的配置或标题中。这应该使您的会话信息“粘性”。只是一个想法!
-
我认为 TimBrownlaw 建议的 session_start 是我需要的,我遇到的问题是我的每个页面都有下拉菜单,其中时区是在用户执行搜索时通过 GET 方法发送的, 我最好有一个页面,用户可以在其中选择他们的时区,它将将该值传递到会话中,并在其他每个页面上启动会话,而不需要时区的下拉菜单,并且可以采用时区从会话中更新我表中的日期时间字段值?
-
TimBrownlaw,是否可以写一个答案来说明我如何实现您的方法?非常感谢
-
我尝试使用 session_start(); $_SESSION['timezone']='$timezone';在 header.php 页面中。我尝试添加 session_start();回声 $_SESSION['timezone'];到需要传入时区值的页面,我得到的唯一输出是标题输出中的 $timezone。我使用 GET 变量设置它的方式会输出一个 URL,例如 sportschedule.xyz/… 这会在我网站的表中正确显示新时区,但是当用户搜索时,我如何让 $_SESSION 自动传递到 URL 字符串中例如按日期。