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;
相关推荐
点心的游戏开发世界7 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
事圆则缓9 小时前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i9 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路9 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
统计学小王子10 小时前
数学建模国赛倒计时4天——《统计模型精讲(混合效应模型R语言实现)》
开发语言·数学建模·r语言
知无不研10 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
郝学胜-神的一滴10 小时前
Qt 高级编程 045:坐标体系深度实战
开发语言·c++·windows·python·qt·程序人生
dsyyyyy110111 小时前
JS复习,难点,易错点总结
开发语言·javascript·ecmascript
“AI国潮设计-小江”12 小时前
Python实战 | SDXL精准控制“普宁英歌舞×星空蛋糕”IP落地,附核心Prompt与商用授权思路
开发语言·人工智能·python·prompt·aigc
小刘在重生~13 小时前
Java常用类|String类详解 + BigDecimal精准计算(含面试题)
java·开发语言·python