duanchao1002 2016-04-04 13:59
浏览 31
已采纳

如何使用日期函数的时间戳参数更改日期和时间?

When I use date() function to display date, it's not according to local time zone. I want it to be according to the local time zone. Can it be done using the timestamp parameter of this function?

Actually I don't know what the timestamp parameter is. How do I use it to alter date and time?

Secondly, is there any other function that can set date time according to our time zone?

Here's what I've tried but it's not working:

<?php
$conn=mysqli_connect("localhost", "root", "", "winkcage");
if(isset($_POST["cmessage"])){
$messages=$_POST["cmessage"];
$sender=$_SESSION["unamsession"];
$receiver="vinaykumar";
$timedate=date("Y-m-d h:i:sa");
$time=date_default_timezone_set("Asia/Calcutta");
$sendquery="INSERT INTO messages(sender, receiver, message, time) VALUES('$sender', '$receiver', '$messages', '$time')";
$sendqueryrun=mysqli_query($conn, $sendquery);
}
?>
  • 写回答

4条回答 默认 最新

  • dtz88967 2016-04-04 14:23
    关注

    Timestamp is nothing more then Unix Time - it's used widely in IT and it's defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

    Default value for timestamp parameter of date function is time(). If it's not according to your timezone you should change the default timezone using date_default_timezone_set() function. List of supported timezones.

    Instead of using function (if you can edit your php.ini) you should set default timezone there. More about it here.

    Also, it's better to use DateTime class if you are running PHP >= 5.2. To get current date in your timezone you could create new DateTime object with two parameters: first would be current time (you can set it to now and it will work) and second being DateTimeZone object with timezone name as parameter, list of available timezone names is available here. Then you can easily convert it to desired format. Example:

    $date = new DateTime( "now", new DateTimeZone( "America/Bogota" ) );
    $date = $date->format( "Y-m-d H:i:s" );
    echo $date; // prints: 2016-04-04 09:07:48
    

    EDIT:

    For your code try:

    date_default_timezone_set( "Asia/Calcutta" );
    $conn = mysqli_connect( "localhost", "root", "", "winkcage" );
    if ( isset( $_POST[ "cmessage" ] ) ){
        $messages = $_POST[ "cmessage" ];
        $sender = $_SESSION[ "unamsession" ];
        $receiver = "vinaykumar";
        $timedate = date( "Y-m-d h:i:sa" );
        $sendquery = "INSERT INTO messages(sender, receiver, message, time) VALUES('$sender', '$receiver', '$messages', '$time')";
        $sendqueryrun = mysqli_query( $conn, $sendquery );
    }
    

    Or with DateTime class:

    $conn = mysqli_connect( "localhost", "root", "", "winkcage" );
    if ( isset( $_POST[ "cmessage" ] ) ){
        $messages = $_POST[ "cmessage" ];
        $sender = $_SESSION[ "unamsession" ];
        $receiver = "vinaykumar";
        $date = new DateTime( "now", new DateTimeZone( "America/Bogota" ) );
        $timedate = $date->format( "Y-m-d H:i:s" );
        $sendquery = "INSERT INTO messages(sender, receiver, message, time) VALUES('$sender', '$receiver', '$messages', '$time')";
        $sendqueryrun = mysqli_query( $conn, $sendquery );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加