2010-12-03 93 views
0

我会要求用户输入开始日期和结束日期。我想要从开始到结束获取所有日期,并获取这些日期之间的天数。如何在PHP中给出开始日期和结束日期。开始和结束日期在php的区别

+0

http://stackoverflow.com/questions/676824/how-to-calculate-the-date-difference-between-2-dates-using-php – ArK 2010-12-03 07:47:35

回答

0

将两个日期转换为时间戳,然后从一个转换到另一个,转换回日期。

0

此代码的最终结果将为天数。 (strtotime(date(“Y-m-d”)) - strtotime(“2010-08-20”))/(60 * 60 * 24); echo $ days;

0

根据输入的用户提供了,我最有可能使用像

$dateOne = (int)(mktime(0, 0, 0, $month1, $day1, $year1)/86400); //Get the first date as a unix timestamp, then convert to days. 
$dateTwo = (int)(mktime(0, 0, 0, $month2, $day2, $year2)/86400); //Get the second date as a unix timestamp, then convert to days. 

// Example 
//$dateOne = (int)(mktime(0,0,0,12,03,2009)/86400); 
//$dateTwo = (int)(mktime(0,0,0,08,19,2011)/86400); 

$daysBetween = $dateTwo-$dateOne; //Calculate days between. 

echo $daysBetween.'<br />'; //Echo the days between. 

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day. 
    echo date('Y-m-d', $dateOne*86400+($i*86400)).'<br />'; 
} 

东西上面的代码将给你所有的日子,包括第一次和最后一次约会。要获得唯一的那些变革之间:

for ($i=1; $i=$daysBetween; $i++) { //Loop through every day between, echo the date of that day. 

到:

for ($i=2; $i<=$daysBetween; $i++) { //Loop through every day between, echo the date of that day. 

已测试,效果很好。

注意:第一个日期必须在最后一个日期之前。

干杯 查理

相关问题