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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀