mysql按新时区中的where类选择curdate()

ffvjumwh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(358)

我更新了下表:

<?php
   $date = new DateTime();
   $date->setTimezone(new DateTimeZone('Asia/Kolkata'));
   $fdate = $date->format('Y-m-d H:i:s');
   $sql="update mytable set logged_on='$fdate' where id='$Id'";
  ?>

然后我试着选择当前日期如下:

<?php
    date_default_timezone_set('Asia/Kolkata');
    $sql="select * from mytable where DATE(logged_on) = CURDATE()";
   ?>

但是curdate()不起作用。请告诉我如何选择当前日期在哪里类。

vom3gejh

vom3gejh1#

这是有效的

<?php
   $date = new DateTime();
   $date->setTimezone(new DateTimeZone('Asia/Kolkata'));
   $fdate = $date->format('Y-m-d H:i:s');
   $sql="select * from mytable where DATE(logged_on) = Date($fdate)" 
?>

但我觉得更好的答案是

<?php
      $con=mysqli_connect("localhost","my_user","my_password","my_db");
      $con->query("SET GLOBAL time_zone = 'Asia/Calcutta'");
      $con->query("SET time_zone = '+05:30'");
      $con->query("SET @@session.time_zone = '+05:30'");
      $sql="select * from mytable where DATE(logged_on) = CURDATE()";
?>

相关问题