PHP获取当前时间戳

2020/6/26 php

php 秒数和毫秒数的时间戳

# 时间戳---秒

  1. php自带函数获取当前时间戳
time();
1
  1. 通过$_SERVER中的REQUEST_TIME元素
$_SERVER['REQUEST_TIME'];
1
  1. 通过strtotime函数
strtotime('now'));
1

1970年1月1日0:00:00到当前的秒数

# 时间戳---毫秒

  1. microtime() 获取毫秒时间戳
//返回当前的毫秒时间戳
function msectime() {
  list($msec, $sec) = explode(' ', microtime());
  $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  return $msectime;
}
1
2
3
4
5
6

sprintf('%.0f', $num) 是输出不含小数部分的浮点数 事情还没有结束,我把时间戳改成毫秒级别后,再次更新数据库数据时,却提示超出范围,原来之前我在数据库中是用int型来存储time()函数获取的秒级别的时间戳,存储范围是够的,改成毫秒级别的,就得改成BIGINT类型了

参考博客 (opens new window)

Last Updated: 2020/11/18 00:41:38