php strtotime
时间: 2023-08-28 19:07:10 浏览: 53
`strtotime()` 函数是一个用于将人类可读的日期时间格式转换为 Unix 时间戳的 PHP 函数。它接受一个表示日期时间的字符串作为参数,并返回该日期时间的 Unix 时间戳。
例如,你可以使用 `strtotime()` 函数将一个日期字符串转换为 Unix 时间戳,如下所示:
```php
$dateString = "2022-01-01 00:00:00";
$timestamp = strtotime($dateString);
echo $timestamp; // 输出:1640995200
```
在上面的例子中,`strtotime()` 函数将字符串 "2022-01-01 00:00:00" 转换为 Unix 时间戳,表示该日期时间距离 1970 年 1 月 1 日 00:00:00 的秒数。
需要注意的是,`strtotime()` 函数对于不同的日期时间格式有一些约束和限制,并且会根据当前时区进行日期时间转换。详细的用法和支持的日期时间格式,请参考 PHP 官方文档中 `strtotime()` 函数的说明。
相关问题
PHP strtotime
PHP中的strtotime函数可以将人类可读的日期时间字符串转换为UNIX时间戳。它的语法如下:
```
strtotime ( string $time [, int $now = time() ] ) : int|false
```
其中 $time 参数是需要转换的日期时间字符串,$now 参数是可选的,默认值为当前时间的UNIX时间戳。函数会返回转换后的UNIX时间戳,如果转换失败则返回false。
例如,以下代码可以将字符串"2022-01-01 00:00:00"转换为UNIX时间戳:
```php
$timestamp = strtotime("2022-01-01 00:00:00");
echo $timestamp;
```
输出结果为:
```
1640995200
```
这个时间戳表示的是2022年1月1日0时0分0秒距离UNIX纪元(1970年1月1日0时0分0秒)的秒数。
phpstrtotime函数怎么用
PHP的strtotime()函数用于将人类可读的日期时间字符串转换为UNIX时间戳。以下是它的语法:
```
strtotime(time_string, [now])
```
其中,time_string 是需要转换的日期时间字符串,now 是可选的,表示当前时间。
例如,如果我们想将“2021-07-15 14:30:00”转换为UNIX时间戳,可以这样做:
```php
$timestamp = strtotime('2021-07-15 14:30:00');
echo $timestamp;
```
输出结果应该是 1626346200,它是从1970年1月1日开始的秒数。
另外,strtotime()函数还支持解析相对日期时间字符串,如“+1 day”代表明天,"-1 week"代表上周等等。例如:
```php
$timestamp = strtotime('+1 day');
echo date('Y-m-d', $timestamp); // 输出明天的日期
```