如果我有兩個日期20-4-2010和22-4-2010 在兩個文本框和 我想的日期是這樣20,21,22,我如何獲得的?如何查找兩個指定日期之間的日期?
10
A
回答
14
我敢肯定這已經回答了幾十億次之前,但無論如何:
$start = strtotime('20-04-2010 10:00');
$end = strtotime('22-04-2010 10:00');
for($current = $start; $current <= $end; $current += 86400) {
echo date('d-m-Y', $current);
}
的10:00
部分是爲了防止代碼跳過或者一天重複由於夏令時。
通過給天數:
for($i = 0; $i <= 2; $i++) {
echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}
隨着PHP5.3
$period = new DatePeriod(
new DateTime('20-04-2010'),
DateInterval::createFromDateString('+1 day'),
new DateTime('23-04-2010') // or pass in just the no of days: 2
);
foreach ($period as $dt) {
echo $dt->format('d-m-Y');
}
7
您可以使用mktime()
。
mktime()對於日期算術和驗證很有用,因爲它會自動計算超出範圍輸入的正確值。
如果增加的天數時,你得到一個有效的日期回來,即使你走過去的一個月結束:
<?php
$day= 25;
$dateEnd = mktime(0,0,0,5,3,2010);
do {
$dateCur = mktime(0,0,0,4,$day,2010);
$day++;
print date('d-m-y', $dateCur) .'<br>';
} while ($dateCur < $dateEnd);
輸出:
25-04-10
26-04-10
27-04-10
28-04-10
29-04-10
30-04-10
01-05-10
02-05-10
03-05-10
1
你可以這樣做:
$start = strtotime("2010-04-20"); // get timestamp for start date.
$end = strtotime("2010-04-22"); // get timestamp for end date.
// go from start timestamp to end timestamp adding # of sec in a day.
for($t=$start;$t<=$end;$t+=86400) {
// get the date for this timestamp.
$d = getdate($t);
// print the date.
echo $d['mday'].'-'.$d['mon'].'-'.$d['year']."\n";
}
輸出:
20-4-2010
21-4-2010
22-4-2010
-1
試試這個,希望它可以幫助幫助
$begin = date("Y-m-d", strtotime($date);
$end = date("Y-m-d", strtotime($date));
$begin = new DateTime($begin);
$end = new DateTime($end);
for ($i = $begin; $i <= $end; $i=$i->modify('+1 day')) {
echo $i->format('Y-m-d');
}
0
/**
* Function to list of weeks start and end.
* @param string strDateFrom (Date From "YYYY-MM-DD")
* @param string strDateTo (Date To "YYYY-MM-DD")
* @return array weeks
*/
function getWeeksBetweenTowDates($date_from, $date_to) {
$startweek = $current_week = date("W", strtotime($date_from));
$endweek = date("W", strtotime($date_to));
$current_year = date("Y", strtotime($date_from));
$current_yearweek = date("Y", strtotime($date_from)) . $startweek;
$end_yearweek = date("Y", strtotime($date_to)) . $endweek;
$start_day = 0;
$end_day = 0;
while ($current_yearweek <= $end_yearweek) {
$dto = new DateTime();
if ($start_day == 0) {
$start_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 0)->format('Y-m-d');
$end_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 6)->format('Y-m-d');
} else {
$start_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 0)->format('Y-m-d');
$end_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 6)->format('Y-m-d');
}
$arr_weeks[sprintf("%02d", $current_week)] = $start_day . " =>" . $end_day;
$last_yearweek_in_year = $current_year . date("W", strtotime('31-12-' . $current_year));
if ($current_yearweek == $last_yearweek_in_year) { //last week in the year
$current_week = 1;
$current_year = ($current_year + 1);
} else {
$current_week = ($current_week + 1);
}
$current_yearweek = $current_year . sprintf("%02d", $current_week);
}
return $arr_weeks;
}
沒命:date_from =二零一五年十月二十日,DATE_TO = 2016年4月15日
$arr_weeks = $this->getWeeksBetweenTowDates($date_from, $date_to);
輸出:
Array
(
[43] => 2015-10-18 =>2015-10-24
[44] => 2015-10-25 =>2015-10-31
[45] => 2015-11-01 =>2015-11-07
[46] => 2015-11-08 =>2015-11-14
[47] => 2015-11-15 =>2015-11-21
[48] => 2015-11-22 =>2015-11-28
[49] => 2015-11-29 =>2015-12-05
[50] => 2015-12-06 =>2015-12-12
[51] => 2015-12-13 =>2015-12-19
[52] => 2015-12-20 =>2015-12-26
[53] => 2015-12-27 =>2016-01-02
[01] => 2016-01-03 =>2016-01-09
[02] => 2016-01-10 =>2016-01-16
[03] => 2016-01-17 =>2016-01-23
[04] => 2016-01-24 =>2016-01-30
[05] => 2016-01-31 =>2016-02-06
[06] => 2016-02-07 =>2016-02-13
[07] => 2016-02-14 =>2016-02-20
[08] => 2016-02-21 =>2016-02-27
[09] => 2016-02-28 =>2016-03-05
[10] => 2016-03-06 =>2016-03-12
[11] => 2016-03-13 =>2016-03-19
[12] => 2016-03-20 =>2016-03-26
[13] => 2016-03-27 =>2016-04-02
[14] => 2016-04-03 =>2016-04-09
[15] => 2016-04-10 =>2016-04-16
)
相關問題
- 1. iPhone:如何查找兩個選定日期之間的日期?
- 2. 查找兩個日期之間發生的日期日期
- 3. 在兩個日期之間的特定日期的查找日期PHP
- 4. Xcode - 如何找到兩個日期之間的中間日期
- 5. 查找值如果日期落在兩個日期之間
- 6. 查找兩個日期之間的日期Jquery/JS
- 7. 在兩個日期之間查找日期(最佳做法)
- 8. SQL - 查找日期是否在兩個日期之間
- 9. 查找日期向量中的日期是否落在兩個日期之間
- 10. 如何檢查日期是否在兩個日期之間?
- 11. 如何檢查日期是否在兩個日期之間mysql?
- 12. 如何查找兩個日期之間的確切月份和日期 - angularjs
- 13. 如何在JavaScript或jQuery datepicker中查找兩個日期之間的日期?
- 14. 如何找到兩日期之間的具體日期
- 15. 如何枚舉兩個日期之間的日期在瞬間
- 16. 查找第一個星期一的日期指定的日期
- 17. MySql查詢兩個日期之間的任何日期是在兩個日期之間
- 18. 如何檢查特定的日期是在兩個不同的日期之間?
- 19. 如何檢查是否一個日期是兩個給定日期之間
- 20. 如何檢查MySQL中兩個給定日期之間的當前日期?
- 21. 如何在Oracle查詢中的兩個給定日期之間選擇日期?
- 22. 在兩個日期之間找到丟失的日期
- 23. 查找SQL Server中兩個日期之間的工作日
- 24. 如何找到兩個日期之間的星期差異
- 25. 查找兩個日期之間的星期列表
- 26. 如何檢查兩個日期之間的日期是星期幾?
- 27. 如何獲取DateTime中的兩個日期之間的日期
- 28. 確定兩個日期之間是否落在兩個日期之間
- 29. 如何查找R中兩個日期之間的差異?
- 30. 如何在jsp中查找兩個日期之間的區別?
+1 Bestest Reply ... – 2010-04-29 12:16:32
對於Date *類爲+1,將其稱爲「花哨」和「矯枉過正」,+1表示除了經常引用的「strtotime」解決方案之外的其他內容。 :) – salathe 2010-04-29 12:31:57
@gordon你能解釋一下爲什麼設置10:00時還有允許那些適當地考慮DST?僅供參考,爲什麼我要問,請參見:http://stackoverflow.com/questions/15302469/problems-with-finding-the-days-between-two-unix-timestamps – Shackrock 2013-03-09 12:25:55