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

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);
}
?>

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 );
}

Try This :

date_default_timezone_set('Asia/Dhaka'); //set the time zone
echo date('Y-m-d h:i:s A'); //print local time of the current time zone

Update

Mysql datetime format is 24H. To insert in mysql database,

$timedate = date('Y-m-d H:i:s');//24H time format

Or,

$sendquery="INSERT INTO messages(sender, receiver, message, time) VALUES('$sender', '$receiver', '$messages', '".date('Y-m-d H:i:s', strtotime($timedate))."')";

You could do something like so:

$d = new DateTime();
$d->setTimezone(new DateTimeZone('Europe/London'));

var_dump($d);

You can also do it in one line, like so:

$d = new DateTime('', new DateTimeZone('Europe/London'));

Output:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2016-04-04 15:19:35.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}

You can change it in the php.ini file. Just search date.timezone and set it to your timezone.

Go to http://php.net/manual/en/timezones.php to search yours.

Or you can use date_default_timezone_set(). For this one, I let you search there : http://php.net/manual/en/function.date-default-timezone-set.php

For example, if you choose Paris, in the php.ini file it would be :

date.timezone = Europe/Paris

with the function, it would be :

date_default_timezone_set('Europe/Paris');

EDIT : Try this code.

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', '$timedate')";
  $sendqueryrun=mysqli_query($conn, $sendquery);
}