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;
相关推荐
BingoGo16 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.0 发布,新增短信服务能力
后端·php
Interview Aid11218 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt1 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源1 天前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
「QT(C++)开发工程师」1 天前
C++ auto 用法详解
开发语言·c++
OPEN-F1 天前
C++STL教程:容器适配器与实用工具
开发语言·c++
yaoxin5211231 天前
507. Java 反射 - 在 BeanFactory 中实现依赖注入
java·开发语言
OPEN-F1 天前
C++模板教程:变参模板、折叠表达式与SFINAE
java·开发语言·c++
有点。1 天前
C++二叉搜索树进阶
开发语言·c++