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;
相关推荐
BingoGo1 天前
PHP 如何利用 Opcache 来实现保护源码
后端·php
BingoGo2 天前
2025 年 PHP 常见面试题整理以及对应答案和代码示例
后端·php
Bruce1233 天前
web专题之php代审(二)
php
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
BingoGo3 天前
PHP-FPM 深度调优指南 告别 502 错误,让你的 PHP 应用飞起来
后端·php
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机