在PHP中的2个Unix时间戳之间寻找日子 [英] Finding days between 2 unix timestamps in php

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

问题描述

我有一个保存事件的数据库.有两个字段开始"和结束",其中包含时间戳.当管理员输入这些日期时,它们只能设置日,月,年.因此,我们只处理包含日,月,年,而非小时,分钟,秒(小时,分钟和秒设置为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天全站免登陆