js和PHP 時間戳與日期轉換

js  時間戳轉日期:

function getYMDhms(time){
	var date = new Date(parseInt(time) * 1000); //獲取一個時間對象  注意:如果是uinx時間戳記得乘於1000。比如php函數time()獲得的時間戳就要乘於1000

//		console.log(date.getFullYear());
	
	/*----------下面是獲取時間日期的方法,需要什麼樣的格式自己拼接起來就好了----------*/
//		date.getFullYear();//獲取完整的年份(4位,1970)
//		date.getMonth();//獲取月份(0-11,0代表1月,用的時候記得加上1)
//		date.getDate();//獲取日(1-31)
//		date.getTime();//獲取時間(從1970.1.1開始的毫秒數)
//		date.getHours();//獲取小時數(0-23)
//		date.getMinutes();//獲取分鐘數(0-59)
//		date.getSeconds();//獲取秒數(0-59)var date = new Date(時間戳); //獲取一個時間對象  注意:如果是uinx時間戳記得乘於1000。比如php函數time()獲得的時間戳就要乘於1000
	
	Y = date.getFullYear() + '-';
	M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	D = date.getDate() + ' ';
	h = (date.getHours()< 10 ? '0'+date.getHours() : date.getHours()) + ':';
	m = (date.getMinutes()< 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
	s = (date.getSeconds()< 10 ? '0'+date.getSeconds() : date.getSeconds()); 
	return Y+M+D+h+m+s;
}


js  日期轉時間戳:

start_date=new Date(current_year+'-'+current_month+'-01 00:00:00');
end_date=new Date(current_year+'-'+(parseInt(current_month)+1)+'-01 00:00:00');

st = Date.parse(start_date)/1000;  //獲取到的時間戳除於1000就可以獲得unix的時間戳了,在傳值給PHP時用得到。
et = Date.parse(end_date)/1000; 


php  日期轉時間戳:



$time = $_POST["time"];
$time = strtotime($time)-8*3600;

php 時間戳轉日期:

date_default_timezone_set('PRC'); // 中國時區
$time=date('Y-m-d H:i:s',$result['time']);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章