我在jquery中获取日期的代码如下:
var pathname = window.location.pathname; // Returns path only
var url = window.location.href; // Returns full URL
var splits =url.split('?');
if(splits[1])
{
var splits2 = splits[1].split('=');
var newDate = splits2[1];
}
else
{
var date = new Date(),
yr = date.getFullYear(),
month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
newDate = yr + '-' + month + '-' + day;
}
此后,我将其发送到php代码:
$.ajax({
type:'post',
url:'<?php echo BASE_URL; ?>dashboard/get_weedays',
async:false,
data:{date : newDate},
success:function(response)
{
var obj = JSON.parse(response);
design_for_week(obj);
}
})
In php i am getting this date and converting it into strtotime
但是它返回了我不同的值。在php中,我正在这样做
$date = $this->input->post("date");
$dt = strtotime($date);
echo $dt;exit;
for date 2018-11-06 timestamp in php is : 1541442600
但是当我通过ajax传递日期并在php上获取日期后,它会返回我timestam,例如:1541442600
两者都不一样,有人可以为此建议我吗?我该如何解决 1541615400
答案 0 :(得分:0)
从客户端传输UTC时间戳,以避免客户端和服务器之间的时区差异。
在javascript中获取UTC时间戳:
var pathname = window.location.pathname; // Returns path only
var url = window.location.href; // Returns full URL
var splits =url.split('?');
if(splits[1])
{
var splits2 = splits[1].split('=');
var getDate = splits2[1];
var datum = Date.parse(getDate); // gets date//
var newDate = Math.floor(datum/1000);
//console.log(newDate);
}
else
{
var date = new Date(new Date().toUTCString());
var jsmonth = date.getMonth()+1;
var yr = date.getFullYear(),
var month = jsmonth < 10 ? '0' + jsmonth : jsmonth, //january is 0
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
var getDate = yr + '-' + month + '-' + day;
var datum = Date.parse(getDate);
var newDate = Math.floor(datum/1000);
//console.log(newDate);
}
答案 1 :(得分:0)
var pathname = window.location.pathname; // Returns path only
var url = window.location.href; // Returns full URL
var splits =url.split('?');
if(splits[1])
{
var splits2 = splits[1].split('=');
var newDate = (new Date(splits2[1])).toUTCString();
date = new Date(newDate).getTime()/1000;
}
else
{
var dates = (new Date()).toUTCString();
date = new Date(dates).getTime()/1000;
}
为此,我们需要这样做,因为您需要隐蔽tiem inti utc,然后将其除以1000,因为这是客户时间