php批量把数组中的日期时间转为时间戳的实现 |
在PHP中,如果你想要将数组中的日期元素批量转换为时间戳,你可以使用 以下是一个简单的示例,说明如何实现这一功能: 示例1:使用strtotime()<?php // 假设你有一个包含日期的数组 $dates = [ "2023-04-01", "2023-04-02 14:30:00", "April 1, 2023" ]; // 创建一个空数组来存储时间戳 $timestamps = []; // 遍历日期数组,使用strtotime()转换每个日期为时间戳 foreach ($dates as $date) { $timestamps[] = strtotime($date); } // 打印结果 print_r($timestamps); ?> 示例2:使用array_map()函数简化操作如果你想要更简洁的方式,可以使用 <?php // 假设你有一个包含日期的数组 $dates = [ "2023-04-01", "2023-04-02 14:30:00", "April 1, 2023" ]; // 使用array_map()和strtotime()批量转换日期为时间戳 $timestamps = array_map('strtotime', $dates); // 打印结果 print_r($timestamps); ?> 注意事项:
到此这篇关于php批量把数组中的日期时间转为时间戳的实现的文章就介绍到这了,更多相关php日期时间转为时间戳内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! |