阅读量:2
在PHP中,将日期或时间字符串转换为时间戳的方法是使用strtotime()函数。下面是一些示例:
- 将日期字符串转换为时间戳:
$date_string = "2022-01-01";
$timestamp = strtotime($date_string);
echo $timestamp; // 输出:1640976000
- 将时间字符串转换为时间戳:
$time_string = "12:30:00";
$timestamp = strtotime($time_string);
echo $timestamp; // 输出:456108600
- 将包含日期的时间字符串转换为时间戳:
$datetime_string = "2022-01-01 12:30:00";
$timestamp = strtotime($datetime_string);
echo $timestamp; // 输出:1640976000
- 使用相对时间字符串转换为时间戳:
$relative_time_string = "+1 day";
$timestamp = strtotime($relative_time_string);
echo $timestamp; // 输出:1640985600(当前时间的时间戳加1天)
请注意,strtotime()函数会根据本地时区和环境设置来解析日期和时间字符串。如果需要指定时区,可以使用date_default_timezone_set()函数来设置。
以上就是关于“php转时间戳如何编写”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm