PHP:获取时间戳,时间,以及相关转换

一、获取当前时间戳

php 复制代码
// 获取当前时间的时间戳(秒级,11位)
$currentTimestampInSeconds = time();

二、秒级(11位)时间戳转换为毫秒级(13位)时间戳

乘以1000即可

php 复制代码
// 转换为毫秒时间戳(13位)
$millisecondsTimestamp = $currentTimestampInSeconds * 1000;

三、将时间戳转换为日期时间字符串

php 复制代码
// 秒级时间戳:假设有一个时间戳
$timestampInSeconds = 1659853800;
// 使用 date 函数将其转换为日期时间字符串
$dateTimeString = date("Y-m-d H:i:s", $timestampInSeconds);

// 毫秒级时间戳:若要以毫秒级时间戳转换,首先除以1000获得秒级时间戳
$timestampInMilliseconds = 1659853800000;
$timestampInSeconds = floor($timestampInMilliseconds / 1000);
// 然后同上,转换为日期时间字符串
$dateTimeString = date("Y-m-d H:i:s", $timestampInSeconds);

四、将时间字符串转换为时间戳

php 复制代码
// 假设有个日期时间字符串
$dateTimeString = "2024-04-09 15:30:00";

// 使用 strtotime 函数将其转换为时间戳(秒级)
$timestampInSeconds = strtotime($dateTimeString);

// 若需要转换为毫秒级时间戳
$timestampInMilliseconds = strtotime($dateTimeString) * 1000;
相关推荐
张太行_40 分钟前
C++中的析构器(Destructor)(也称为析构函数)
开发语言·c++
SteveKenny3 小时前
Python 梯度下降法(六):Nadam Optimize
开发语言·python
Hello.Reader4 小时前
深入浅出 Rust 的强大 match 表达式
开发语言·后端·rust
xrgs_shz6 小时前
MATLAB的数据类型和各类数据类型转化示例
开发语言·数据结构·matlab
来恩10039 小时前
C# 类与对象详解
开发语言·c#
komo莫莫da9 小时前
寒假刷题Day19
java·开发语言
ElseWhereR9 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
字节全栈_rJF10 小时前
概述、 BGP AS 、BGP 邻居、 BGP 更新源 、BGP TTL 、BGP路由表、 BGP 同步
网络·智能路由器·php
※DX3906※10 小时前
cpp实战项目—string类的模拟实现
开发语言·c++