PHP可以使用strtotime()函数将日期字符串转换为时间戳,然后通过计算两个时间戳之间的差值来得到天数。具体步骤如下:
$timestamp1 = strtotime($dates[0]);
$timestamp2 = strtotime($dates[1]);
$diff = abs($timestamp2 - $timestamp1);
$days = floor($diff / 86400);
完整的代码示例如下:
$dates = array("2022-01-01", "2022-01-10");
$timestamp1 = strtotime($dates[0]);
$timestamp2 = strtotime($dates[1]);
$diff = abs($timestamp2 - $timestamp1);
$days = floor($diff / 86400);
echo "两个日期之间的天数为:" . $days . "天";
以上代码将输出:
两个日期之间的天数为:9天
推荐的腾讯云相关产品:腾讯云云服务器(CVM),产品介绍链接地址:https://cloud.tencent.com/product/cvm
领取专属 10元无门槛券
手把手带您无忧上云