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).
Hay,我有一个数据库举办活动。有两个字段'start'和'end',它们包含时间戳。当管理员输入这些日期时,他们只能设置日,月,年。所以我们只处理包含天,月,年,而不是小时,分钟,秒(小时,分钟和秒设置为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.
我有一个事件,开始时间为1262304000,结束时间为1262908800.这些转换为2010年1月1日和2010年1月8日。我如何获得这些时间戳之间的所有日期?我希望能够返回2010年1月2日(1262390400),2010年1月3日(1262476800)..一直到结束邮票。这些事件可以跨越到不同月份,例如5月28日至6月14日。
Any ideas how to do this?
任何想法如何做到这一点?
8 个解决方案
#1
39
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 :
然后,您可以使用for循环来检索日期:
$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).
同样,如果您不知道哪个时间戳最小,则可以使用min()函数(strtotime中的第二个参数)。
#2
2
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.
我认为快速解决此问题的方法是从end_stamp中减去几天的秒数,直到你到达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.
这应该涵盖任何时间范围,即使它超过一个月。
#3
2
Try this:
尝试这个:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
希望这可以帮助 !
#4
1
$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.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
http://www.phpf1.com/tutorial/php-date-difference.html
#5
1
**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";
#6
0
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.
顺便说一句,1262304000是12月31日,而不是1月1日。
#7
0
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result
得到两个日期的差异并除以86400.abs(($ date1 - $ date2)/ 86400)将产生所需的结果
#8
0
$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.
请注意,range()函数是包含在内的。
#1
39
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 :
然后,您可以使用for循环来检索日期:
$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).
同样,如果您不知道哪个时间戳最小,则可以使用min()函数(strtotime中的第二个参数)。
#2
2
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.
我认为快速解决此问题的方法是从end_stamp中减去几天的秒数,直到你到达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.
这应该涵盖任何时间范围,即使它超过一个月。
#3
2
Try this:
尝试这个:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
希望这可以帮助 !
#4
1
$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.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
http://www.phpf1.com/tutorial/php-date-difference.html
#5
1
**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";
#6
0
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.
顺便说一句,1262304000是12月31日,而不是1月1日。
#7
0
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result
得到两个日期的差异并除以86400.abs(($ date1 - $ date2)/ 86400)将产生所需的结果
#8
0
$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.
请注意,range()函数是包含在内的。