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

发布于 2024-09-30 15:09:12 字数 360 浏览 11 评论 0原文

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

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

有什么想法可以做到这一点吗?

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).

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眼前雾蒙蒙 2024-10-07 15:09:12

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

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

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

$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 中的第二个参数)。

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;

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 />';
}

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

暖风昔人 2024-10-07 15:09:12

我认为对此的快速解决方法是从 end_stamp 中减去一天的秒数,直到到达 start_tag。

//1 day = 86400 seconds

我会构建一个日期数组以供以后使用。

编辑(示例)

$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
    $days[] = date('M j Y', $end_time);

    $end_time -= $difference;
}

这应该涵盖任何时间范围,即使是超过几个月。

I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.

//1 day = 86400 seconds

I would build an array of the days to use later.

EDIT (example)

$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
    $days[] = date('M j Y', $end_time);

    $end_time -= $difference;
}

This should cover any time frame even if its over a bunch of months.

一个人的旅程 2024-10-07 15:09:12

试试这个:

while($date_start <= $date_end) {
    echo date('M d Y', $date_start) . '<br>';
    $date_start = $date_start + 86400;
}

希望这有帮助!

Try this:

while($date_start <= $date_end) {
    echo date('M d Y', $date_start) . '<br>';
    $date_start = $date_start + 86400;
}

Hope this helps !

彡翼 2024-10-07 15:09:12
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";


echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";

http://www.plus2net.com/php_tutorial/date-diff.php

http://www.phpf1.com/tutorial/php-date-difference.html

$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";


echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";

http://www.plus2net.com/php_tutorial/date-diff.php

http://www.phpf1.com/tutorial/php-date-difference.html

蓝眼睛不忧郁 2024-10-07 15:09:12
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/

请注意,range() 函数是包容性的。

$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/

Note that the range() function is inclusive.

沩ん囻菔务 2024-10-07 15:09:12
    **This is a very simple code for find days hours minutes and seconds in php**

    $dbDate = strtotime("".$yourbdDate.""); // Database date
    $endDate = time();    // current time
    $diff = $endDate - $dbDate; /// diffrence

    $days = floor($diff/86400);  ///  number of days 
    $hours = floor(($diff-$days*86400)/(60 * 60));  ////  number of hours
    $min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute


    $second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes

    if($days > 0) echo $days." Days ago";
    elseif($hours > 0) echo $hours." Hours ago";
    elseif($min > 0) echo $min." Minute ago";
    else echo "Just second ago";
    **This is a very simple code for find days hours minutes and seconds in php**

    $dbDate = strtotime("".$yourbdDate.""); // Database date
    $endDate = time();    // current time
    $diff = $endDate - $dbDate; /// diffrence

    $days = floor($diff/86400);  ///  number of days 
    $hours = floor(($diff-$days*86400)/(60 * 60));  ////  number of hours
    $min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute


    $second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes

    if($days > 0) echo $days." Days ago";
    elseif($hours > 0) echo $hours." Hours ago";
    elseif($min > 0) echo $min." Minute ago";
    else echo "Just second ago";
贩梦商人 2024-10-07 15:09:12

像这样的东西吗?

$day = $start;
while ($day < $end) {
        $day += 86400;
        echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}

顺便说一句,1262304000 是 12 月 31 日,而不是 1 月 1 日。

Something like this?

$day = $start;
while ($day < $end) {
        $day += 86400;
        echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}

By the way, 1262304000 is Dec 31, not Jan 1.

此刻的回忆 2024-10-07 15:09:12

获取两个日期的差值并将其除以 86400。abs(($date1 - $date2) / 86400) 将产生所需的结果

get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文