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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题