简体   繁体   English

即使在 php 中设置时区后,服务器中的时间戳也给出错误的时间

[英]timestamp in server giving wrong time even after setting timezone in php

i have a website which adds values to table after the user fills the form.我有一个网站,它在用户填写表格后向表格添加值。 My server is outside India, so when i put current timestamp its not giving me correct Indian time, so to fix it i have added Indian timezone in my php file.我的服务器在印度境外,所以当我输入当前时间戳时,它没有给我正确的印度时间,所以为了修复它,我在我的 php 文件中添加了印度时区。 the column default value is still "Currenttimestamp" The complete code is like below:列默认值仍然是“Currenttimestamp”完整的代码如下:

<?php

session_start();

error_reporting(0);
include('includes/dbconnection.php');
if (strlen($_SESSION['cvmsaid']==0)) {
  header('location:logout.php');
  } else{
    if(isset($_POST['submit']))
  {



$cvmsaid=$_SESSION['cvmsaid'];
 $fullname=$_POST['fullname'];

$mobnumber=$_POST['mobilenumber'];
$email=$_POST['email'];
$add=$_POST['address'];
$whomtomeet=$_POST['whomtomeet'];
$department=$_POST['department'];
$reasontomeet=$_POST['reasontomeet'];

$query=mysqli_query($con,"set session time_zone='Asia/Kolkata'");
$query=mysqli_query($con, "insert into tblvisitor(FullName,Email,MobileNumber,Address,WhomtoMeet,Deptartment,ReasontoMeet) value('$fullname','$email','$mobnumber','$add','$whomtomeet','$department','$reasontomeet')");

    if ($query) {
    $msg="Visitors Detail has been added.";
  }
  else
    {
      $msg="Something Went Wrong. Please try again";
    }


}

?>
  <!DOCTYPE html>
  <html lang="en">

  <head>


  </head>

  <body class="animsition">
    <div class="page-wrapper">
      <!-- HEADER MOBILE-->
      <?php include_once('includes/sidebar.php');?>

      <div class="page-container">
        <!-- HEADER DESKTOP-->
        <?php include_once('includes/header.php');?>
        <!-- HEADER DESKTOP-->

        <!-- MAIN CONTENT-->
        <div class="main-content">
          <div class="section__content section__content--p30">
            <div class="container-fluid">
              <div class="row">

                <div class="col-lg-12">
                  <div class="card">
                    <div class="card-header">
                      <strong>Add</strong> New Visitor
                    </div>
                    <div class="card-body card-block">
                      <form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
                        <p style="font-size:16px; color:red" align="center">
                          <?php if($msg){
    echo $msg;
  }  ?> </p>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="text-input" class=" form-control-label">Full Name</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="text" id="fullname" name="fullname" placeholder="Full Name" class="form-control" required="">

                          </div>
                        </div>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="email-input" class=" form-control-label">Email Input</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="email" id="email" name="email" placeholder="Enter Email" class="form-control" required="">

                          </div>
                        </div>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="password-input" class=" form-control-label">Phone Number</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="text" id="mobilenumber" name="mobilenumber" placeholder="Mobile Number" class="form-control" maxlength="10" required="">

                          </div>
                        </div>

                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="textarea-input" class=" form-control-label">Address</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <textarea name="address" id="address" rows="9" placeholder="Enter Visitor Address..." class="form-control" required=""></textarea>
                          </div>
                        </div>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="password-input" class=" form-control-label">Whom to Meet</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="text" id="whomtomeet" name="whomtomeet" placeholder="Whom to Meet" class="form-control" required="">

                          </div>
                        </div>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="password-input" class=" form-control-label">Company</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="text" id="department" name="department" placeholder="Company Name" class="form-control" required="">

                          </div>
                        </div>
                        <div class="row form-group">
                          <div class="col col-md-3">
                            <label for="password-input" class=" form-control-label">Reason To Meet</label>
                          </div>
                          <div class="col-12 col-md-9">
                            <input type="text" id="reasontomeet" name="reasontomeet" placeholder="Reason To Meet" class="form-control" required="">

                          </div>
                        </div>

                        <div class="card-footer">
                          <p style="text-align: center;"><button type="submit" name="submit" id="submit" class="btn btn-primary btn-sm">Submit
                                        </button></p>

                        </div>
                      </form>
                    </div>

                  </div>

                </div>

              </div>


              <?php include_once('includes/footer.php');?>
            </div>
          </div>
        </div>
      </div>
    </div>


  </body>

  </html>
  <!-- end document-->
  <?php }  ?>

Still am not getting the Indian timestamp, can anyone please tell me how to fix it.仍然没有得到印度时间戳,谁能告诉我如何解决它。 Thanks in advance提前致谢

If the column is a TIMESTAMP, then all you need to do on the connection, $con , before retrieving the column, is execute the following query:如果该列是 TIMESTAMP,那么在检索该列之前,您需要对连接$con执行的所有操作就是执行以下查询:

set session time_zone='Asia/Kolkata'

This works for whatever language you are using.这适用于您使用的任何语言。 You should not have do any any manipulations in PHP on the returned timestamp as far as time zone conversions;就时区转换而言,您不应在 PHP 中对返回的时间戳进行任何操作; these will be done by MySql for you.这些将由 MySql 为您完成。

By the way, you should use prepared statements.顺便说一句,您应该使用准备好的语句。 As you currently are executing your query, you are leaving yourself open to a possible SQL Injection attack.当您当前正在执行查询时,您将面临可能的 SQL 注入攻击。

Try to create a custom php.ini in your public_html folder with your new config尝试使用新配置在public_html文件夹中创建自定义php.ini

and add to it并添加到它

date.timezone="Asia/Kolkata"

after editing try to check if change correct by编辑后尝试检查更改是否正确

echo date_default_timezone_get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM