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