记住用户时区选择,并将时区更改应用于所有页面上的日期时间字段

时间:2016-10-13 12:49:05

标签: php mysql date datetime

在我的数据库中,我在其中一个表中有一个dateandtime字段,它正确显示数据库中我想要的行的日期和时间。由于我可能会从许多不同的国家/地区获得用户,因此我需要允许用户选择他们的时区,以便将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>    

非常感谢任何阅读和/或建议如何实现解决方案或引导我朝着正确的方向寻求帮助的人

1 个答案:

答案 0 :(得分:-1)

我认为在这个问题上最正确的方法如下: - 在数据库或cookie中保存每个用户的时区(如果您没有经过身份验证的用户) - 在php和javascript中创建一个全局变量,您将在您的网站上使用名为user_date的全局变量 - 在页面的开头使user_date等于DateTime +时区 您必须拥有一个变量,并且每次进行日期计算时都使用该时区。

此外,我将在这里留下我的另一个答案,关于如何在浏览器中直接实现这一点,没有php和数据库关系。

我认为您可以使用javascript自动执行此操作。 stackoverflow.com/questions/1091372 / ...您可以获取浏览器时区,它基本上是用户计算机的时区,您可以使用它。另一个有用的库:momentjs.com/timezone