在php中查找2个unix时间戳之间的天数

编程入门 行业动态 更新时间:2024-10-10 02:22:43
本文介绍了在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时间戳之间的天数

本文发布于:2023-05-25 19:51:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!