如何使用PHP的strtotime函数把时间字符串转换为时间戳?
strtotime函数是PHP中非常常用的日期时间解析函数,可以将一个日期时间的字符串转换为时间戳(以 Unix 时间戳的形式表示)。在这篇文章中,我们将介绍如何使用strtotime函数将时间字符串转换为时间戳。
PHP strtotime函数
strtotime函数是PHP日期时间处理函数库中的一个函数,用来将一个日期时间的字符串转换为时间戳。该函数的一般格式如下:
strtotime(string $time, int $now = time())
该函数接收一个包含日期时间的字符串作为参数(必选),以及一个可选的 Unix 时间戳作为第二个参数,默认为当前时间戳。该函数返回一个表示 Unix 时间戳的整数值,或者在转换失败时返回FALSE。
strtotime函数能够支持的日期时间格式包括以下几种:
- YYYY-MM-DD
- YYYY/MM/DD
- MM/DD/YYYY
- DD-MM-YYYY
- DD/MM/YYYY
- YYYY-MM-DD HH:MM:SS
- YYYY/MM/DD HH:MM:SS
- MM/DD/YYYY HH:MM:SS
- DD-MM-YYYY HH:MM:SS
- DD/MM/YYYY HH:MM:SS
- now
- +3 days
- yesterday
- last Monday
- next Sunday
- etc.
如何使用strtotime函数将时间字符串转换为时间戳?
下面是一个示例,展示如何使用strtotime函数将时间字符串转换为时间戳。
<?php $time_string = "2021-10-20 10:30:00"; $time_stamp = strtotime($time_string); echo "时间字符串: ".$time_string."<br>"; echo "时间戳: ".$time_stamp."<br>"; ?>
执行以上代码,输出结果为:
时间字符串: 2021-10-20 10:30:00 时间戳: 1634712600
使用strtotime函数将时间字符串转换为时间戳的过程非常简单,只需要在函数中传递时间字符串作为参数即可,返回值即为时间戳。注意事项如下:
- $time_string必须是合法的时间字符串格式,否则strtotime函数会返回FALSE。
- 如果$now参数未指定,则默认为当前时间戳(time()函数的返回值)。
- strtotime函数将日期时间字符串转换为UTC Unix时间戳,即格林威治时间1970年1月1日零点零刻(UTC/GMT+0),所以需要按照时区转换的需要进行时间的加减。
再看一个例子,下面代码中将当前时间加上3小时,并将其转换为时间戳。
<?php
$now = time(); //当前时间戳
$after_3_hours = strtotime('+3 hours', $now); //当前时间加上3小时
echo "当前时间戳: ".$now."<br>";
echo "3小时后时间戳: ".$after_3_hours;
?>
执行以上代码,输出结果:
当前时间戳: 1634713478 3小时后时间戳: 1634724278
上述代码中,我们首先获取了当前时间戳,然后使用strtotime函数将当前时间加上3小时,最后输出了当前时间戳和3小时后的时间戳。
strtotime函数的用途
strtotime函数不仅可以将时间字符串转换为时间戳,还可以在很多场合下使用,例如:
- 格式化输出日期时间
- 计算给定日期时间之间的时间差
- 比较两个日期时间的先后
- 将Unix时间戳转换为日期时间字符串
- 判断日期时间是否在一个给定的时间段内
- 等等
下面让我们来看看具体的应用。
1. 格式化输出日期时间
strtotime函数除了将时间字符串转换为时间戳,还可以将时间戳转换为指定格式的日期时间字符串。基本格式如下:
date(string $format, int $timestamp = time())
该函数接受两个参数:要转换的时间戳(可选,默认为当前时间戳)和格式化字符串的格式,表示要将时间戳格式化成的日期时间字符串。下面是一个例子:
<?php
$timestamp = time();
echo "当前时间戳:" . $timestamp . "<br>";
echo "默认格式日期时间字符串:" . date('Y-m-d H:i:s', $timestamp) . "<br>";
echo "ISO 8601 格式日期时间字符串:" . date('c', $timestamp) . "<br>";
echo "RFC 2822 格式日期时间字符串:" . date('r', $timestamp) . "<br>";
echo "Unix 时间戳形式日期时间字符串:" . date('U', $timestamp);
?>
以上代码输出以下结果:
当前时间戳:1635026125 默认格式日期时间字符串:2021-10-24 11:42:05 ISO 8601 格式日期时间字符串:2021-10-24T11:42:05+07:00 RFC 2822 格式日期时间字符串:Sun, 24 Oct 2021 11:42:05 +0700 Unix 时间戳形式日期时间字符串:1635026125
2. 计算给定日期时间之间的时间差
使用strtotime函数,可以计算给定两个日期时间之间的时间差。比如,计算两个时间段之间的差距:
<?php $start_time = "2021-10-01 10:00:00"; $end_time = "2021-10-10 10:00:00"; $start_time_stamp = strtotime($start_time); $end_time_stamp = strtotime($end_time); $diff = $end_time_stamp - $start_time_stamp; $days = floor($diff / (60 * 60 * 24)); echo "两个时间之间相差 " . $days . " 天。"; ?>
以上代码输出以下结果:
两个时间之间相差 9 天。
3. 比较两个日期时间的先后
使用strtotime函数也可以比较两个日期时间之间先后顺序。例如:
<?php
$date1 = "2021-10-01";
$date2 = "2021-10-05";
if(strtotime($date1) < strtotime($date2)){
echo $date1 . " 在 " . $date2 . " 之前。";
} else {
echo $date1 . " 在 " . $date2 . " 之后。";
}
?>
以上代码输出以下结果:
2021-10-01 在 2021-10-05 之前。
4. 将Unix时间戳转换为日期时间字符串
使用date函数可以将Unix时间戳转换为日期时间字符串。例如:
<?php
$timestamp = 1635026922;
echo "Unix时间戳:" . $timestamp . "<br>";
echo "当前格式日期时间字符串:" . date('Y-m-d H:i:s', $timestamp);
?>
以上代码输出以下结果:
Unix时间戳:1635026922 当前格式日期时间字符串:2021-10-24 11:55:22
5. 判断日期时间是否在一个给定的时间段内
再看一个例子,下面代码演示了如何使用strtotime函数检查给定的日期时间是否在指定日期时间范围内。
<?php
$start_time = "2021-10-01 10:00:00";
$end_time = "2021-10-10 10:00:00";
$target_time = "2021-10-05 10:00:00";
if(strtotime($target_time) >= strtotime($start_time) && strtotime($target_time) <= strtotime($end_time)){
echo $target_time . " 在 " . $start_time . " 和 " . $end_time . " 之间。";
} else {
echo $target_time . " 不在 " . $start_time . " 和 " . $end_time . " 之间。";
}
?>
以上代码输出以下结果:
2021-10-05 10:00:00 在 2021-10-01 10:00:00 和 2021-10-10 10:00:00 之间。
总结
strtotime函数是PHP中非常强大和方便的日期时间
