php时间戳常用

昨天0时时间戳
$yesterday_zero = strtotime(date('Y-m-d')) - 3600 * 24;
昨天此时时间戳
$yesterday_now = strtotime('-1 day');
本周一时间戳
$week_this_monday = strtotime('last Monday');
明天时间戳
$tomorrow = strtotime("+1 day");
上周一时间戳
$week_last_monday = strtotime('last Monday') - 3600 * 24 * 7;
上周日时间戳
$week_last_sunday = strtotime('last Monday') - 3600 * 24;
本月第一天时间戳
$month_first = strtotime(date("Y") . "-" . date("m") . "-1");
本月最后一天时间戳
$month_last = strtotime(date("Y") . "-" . date("m") . "-" . date("t"));
获取上个月第一天及最后一天
echo date('Y-m-01', strtotime('-1 month')); 
echo "<br/>"; 
echo date('Y-m-t', strtotime('-1 month'));
### 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、付费专栏及课程。

余额充值