温馨提示×

php转时间戳如何编写

PHP
小樊
83
2024-11-21 06:37:12
栏目: 编程语言
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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()函数来设置。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:php转时间戳如何实现

0