在php中查找2个unix时间戳之间的天数 [英] Finding days between 2 unix timestamps in php

查看:24
本文介绍了在php中查找2个unix时间戳之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个保存事件的数据库.有 2 个字段开始"和结束",它们包含时间戳.当管理员输入这些日期时,他们只能设置日、月、年.所以我们只处理包含天、月、年的戳记,而不是小时、分钟、秒(小时、分钟和秒设置为 0,0,0).

Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).

我有一个事件,开始时间为 1262304000,结束时间为 1262908800.这些转换为 2010 年 1 月 1 日和 2010 年 1 月 8 日.我如何获得这些时间戳之间的所有天数?我希望能够返回 2010 年 1 月 2 日 (1262390400)、2010 年 1 月 3 日 (1262476800) .. 一直到最后的邮票.这些事件可能会跨越不同的月份,比如 5 月 28 日到 6 月 14 日.

I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.

任何想法如何做到这一点?

Any ideas how to do this?

推荐答案

你只需要计算两个日期之间的秒数,然后除以得到天数:

You just have to calculate the number of seconds between the two dates, then divide to get days :

$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;

然后,您可以使用 for 循环来检索日期:

Then, you can use a for loop to retrieve the dates :

$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;

for ($i = 1; $i < $numDays; $i++) {
    echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}

同样,如果您不知道哪个时间戳最小,您可以使用 min() 函数(strtotime 中的第二个参数).

Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).

这篇关于在php中查找2个unix时间戳之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆