js
// 时间转成时间戳
function utiem (ymdhis) {
timestamp = new Date(Date.parse(ymdhis));
timestamp = timestamp.getTime();
timestamp = timestamp/1000;
return timestamp;
}
// 秒转换成day、hour、minutes、seconds
function MillisecondToDate(msd) {
var time = parseFloat(msd);
if (null != time && "" != time) {
if (time > 60 && time < 60 * 60) {
time = parseInt(time / 60.0) + ":" + parseInt((parseFloat(time / 60.0) -
parseInt(time / 60.0)) * 60) + "秒";
}
else if (time >= 60 * 60 && time < 60 * 60 * 24) {
time = parseInt(time / 3600.0) + ":" + parseInt((parseFloat(time / 3600.0) -
parseInt(time / 3600.0)) * 60) + ":" +
parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
}
else {
time = parseInt(time) + "秒";
}
}
return time;
}
//UTC时间转为北京时间
function formatUTC(utc_datetime) {
// 转为正常的时间格式 年-月-日 时:分:秒
var T_pos = utc_datetime.indexOf('T');
var Z_pos = utc_datetime.indexOf('Z');
var year_month_day = utc_datetime.substr(0,T_pos);
var hour_minute_second = utc_datetime.substr(T_pos+1,Z_pos-T_pos-1);
var new_datetime = year_month_day+" "+hour_minute_second; // 2017-03-31 08:02:06
// 处理成为时间戳
timestamp = new Date(Date.parse(new_datetime));
timestamp = timestamp.getTime();
timestamp = timestamp/1000;
// 增加8个小时,北京时间比utc时间多八个时区
var timestamp = timestamp+8*60*60;
// 时间戳转为时间
// var beijing_datetime = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
return timestampToTime(timestamp); // 2017-03-31 16:02:06
}
function timestampToTime(timestamp,num=1) {
timestamp = timestamp+'';
timestamp = timestamp.length==10?timestamp*1000:timestamp;
var date = new Date(timestamp);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
if(num==0){
return y + '-' + m + '-' + d;
}else{
return y + '-' + m + '-' + d +' '+ h +':'+ minute +':' + second;
}
}
php
/**
* 标准时间转UTC时间 增加8个小时
*/
public function dateArUTC($time)
{
// 增加8个小时,北京时间比utc时间多八个时区
// var timestamp = timestamp+8*60*60;
// $time='2019-1-18 00:00:00';
$unix=strtotime($time);
// $utc=date('Y-m-dTH:i:sZ', $unix);
$utc=date('Y-m-d\TH:i:s\Z', $unix-8*60*60);
return $utc;
}
/**
* 标准时间转UTC时间 不增加8个小时
*/
public function dateArUTC_new($time)
{
// 增加8个小时,北京时间比utc时间多八个时区
// var timestamp = timestamp+8*60*60;
// $time='2019-1-18 00:00:00';
$unix=strtotime($time);
// $utc=date('Y-m-dTH:i:sZ', $unix);
$utc=date('Y-m-d\TH:i:s\Z', $unix);
return $utc;
}