Home >Backend Development >PHP Tutorial >js and PHP timestamp and date conversion
js Convert timestamp to date:
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 Convert date to timestamp:
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; //The obtained timestamp is divided by 1000 to get the unix timestamp, which is used when passing the value to PHP get.
et = Date.parse(end_date)/1000;
php Date to timestamp:
$time = $_POST["time"];
$time = strtotime($time )-8*3600;
php timestamp to date:
date_default_timezone_set('PRC'); // China time zone
$time=date('Y-m-d H:i:s',$result[ 'time']);
The above introduces js and PHP timestamp and date conversion, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.