php获得昨天0点的时间戳

本文介绍了如何使用PHP的strtotime函数来获取昨天不同时间点的时间戳,包括昨天0点、晚上24点及23:59:59的时间戳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获得昨天0点时间戳

strtotime('yesterday')

获得昨天晚上24点整时间戳

strtotime(date('Y-m-d'))

获得昨天晚上23:59:59时间戳

strtotime(date('Y-m-d')) - 1
### PHP 中的时间戳使用方法 #### 获取基本时间戳 PHP 提供了 `time()` 函数来获取当前 Unix 时间戳,即自1970年1月1日以来的秒数。此函数返回的是整数值。 ```php $current_timestamp = time(); echo "Current timestamp: " . $current_timestamp; ``` 对于更精确的需求,可以利用 `microtime(true)` 来获得带毫秒精度的时间戳[^2]: ```php $milliseconds_timestamp = round(microtime(true) * 1000); echo "Milliseconds timestamp: " . $milliseconds_timestamp; ``` #### 创建特定日期的时间戳 为了创建指定日期的时间戳,`mktime()` 是一个常用的选择。该函数允许通过传递小时、分钟、秒钟、月份、天数以及年份参数来构建相应时刻的时间戳[^1]。 例如,要得到今天午夜(00:00:00)的时间戳: ```php $today_start = mktime(0, 0, 0, date("n"), date("j"), date("Y")); echo "Today start at midnight: " . $today_start; ``` 同样地,也可以计算昨天同一时刻或者上个月第一天等特殊时间节时间戳。 #### 转换时间戳为可读格式 当拥有一个时间戳并希望将其呈现为人容易理解的形式时,则需要用到诸如 `date()` 或者 `getdate()` 这样的辅助工具[^3]。 ```php // 将时间戳转成标准日期字符串形式 $formatted_date = date('Y-m-d H:i:s', $timestamp); // 或者分解成数组结构 $date_components = getdate($timestamp); print_r($date_components); ``` 以上就是有关于 PHP 处理时间戳的一些基础操作介绍,在实际开发过程中可以根据具体需求灵活运用这些功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值