0

i write code for event calendar in jQuery plugin when i send data from ajax to php for store event in database my time is different in JavaScript and php

my time in JavaScript

Tue Jan 02 2018 09:00:00 GMT+0530 (India Standard Time)

when i get this date time in php my time is

2018-01-02T03:30:00.000Z

i have i can get exact date time which i send from ajax

my ajax code

$.ajax({
 type:"POST",
 url:"eventCreate.php",
 data:{data:enc},
 cache: false,
 success:function(result){
   alert(result);
 }
});

data i have to pass

{start: Tue Jan 02 2018 09:00:00 GMT+0530 (India Standard Time), end: Tue Jan 02 2018 09:30:00 GMT+0530 (India Standard Time), id: 10, date: Tue Jan 02 2018 09:00:00 GMT+0530 (India Standard Time), title: "", …}
body
:
""
date
:
Tue Jan 02 2018 09:00:00 GMT+0530 (India Standard Time) {}
end
:
Tue Jan 02 2018 09:30:00 GMT+0530 (India Standard Time) {}
id
:
10
start
:
Tue Jan 02 2018 09:00:00 GMT+0530 (India Standard Time) {}
title
:
""
__proto__
:
Object

and now how to get exact datetime in php

my php code

<?php
$con=mysqli_connect("localhost","root","","test");
$data = json_decode($_POST['data']);

$query="insert into event_calendar (`event_id`,`start_time`,`end_time`,`date`,`title`,`body`)
     values('".$data->id."','".$data->start."','".$data->end."','".$data->date."','".$data->title."','".$data->body."')";

echo $query;     
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • create a variable and assign the time value in that and pass that. – urfusion Nov 23 '17 at 06:36
  • Post your php code, too. – mega6382 Nov 23 '17 at 06:37
  • Javascript is the clients time, PHP is the servers time. You can send the time to PHP ( from the client ) and then convert it using PHP's date time funcitons. – ArtisticPhoenix Nov 23 '17 at 06:39
  • 1
    You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Nov 23 '17 at 06:42
  • You do get the exact same time. JS shows it in `GMT+0530` and PHP shows it in `GMT`. Same date/time, different time zones. Saving it normalized as GMT is a good thing. Then you decide what time zone it should be when you present the date. – M. Eriksson Nov 23 '17 at 06:43
  • sorry its `$data->date` not `$date` @mega6382 – Bhargav Chudasama Nov 23 '17 at 06:44

2 Answers2

0

Use this code to change the default time zone in php

date_default_timezone_set('Indian/Comoro');

set this code in the top of your php file.

also you can find all supported php time zones in here

Also be careful javascript is a client side programming language, so the timezone of your php code might get changed depending on user country!

Try to get javascript time from your server.

Adam
  • 6,447
  • 1
  • 12
  • 23
0

Set your local time zone in your PHP file, using date_default_timezone_set()

<?php
// set local timezone
date_default_timezone_set("Asia/Kolkata");
$con=mysqli_connect("localhost","root","","test");
$data = json_decode($_POST['data']);

$query="insert into event_calendar (`event_id`,`start_time`,`end_time`,`date`,`title`,`body`)
 values('".$data->id."','".$data->start."','".$data->end."','".$data->date."','".$data->title."','".$data->body."')";

echo $query; 

Asian time zones

Junius L
  • 15,881
  • 6
  • 52
  • 96