温馨提示×

php转时间戳如何编写

PHP
小樊
100
2024-11-21 06:37:12
栏目: 编程语言

在PHP中,将日期或时间字符串转换为时间戳的方法是使用strtotime()函数。下面是一些示例:

  1. 将日期字符串转换为时间戳:
$date_string = "2022-01-01"; $timestamp = strtotime($date_string); echo $timestamp; // 输出:1640976000 
  1. 将时间字符串转换为时间戳:
$time_string = "12:30:00"; $timestamp = strtotime($time_string); echo $timestamp; // 输出:456108600 
  1. 将包含日期的时间字符串转换为时间戳:
$datetime_string = "2022-01-01 12:30:00"; $timestamp = strtotime($datetime_string); echo $timestamp; // 输出:1640976000 
  1. 使用相对时间字符串转换为时间戳:
$relative_time_string = "+1 day"; $timestamp = strtotime($relative_time_string); echo $timestamp; // 输出:1640985600(当前时间的时间戳加1天) 

请注意,strtotime()函数会根据本地时区和环境设置来解析日期和时间字符串。如果需要指定时区,可以使用date_default_timezone_set()函数来设置。

0