Si tengo dos fechas 20-4-2010 y 22-4-2010 en dos cuadros de texto y Quiero que las fechas sean así 20, 21, 22. ¿Cómo obtengo eso?¿Cómo encontrar las fechas entre dos fechas especificadas?
10
A
Respuesta
14
Estoy bastante seguro esto ha sido respondido un billón de veces antes, pero de todos modos:
$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);
}
La parte 10:00
es para evitar que el código omita o repita un día debido al horario de verano.
Al dar el número de días:
for($i = 0; $i <= 2; $i++) {
echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}
Con 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
Puede usar mktime()
.
mktime() es útil para realizar la aritmética de fechas y la validación, ya que calculará automáticamente el valor correcto para la entrada fuera de rango.
Si incrementar el número de días se obtiene una fecha válida atrás, incluso si vas más allá del final del mes:
<?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);
Salida:
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
Usted puede hacer:
$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";
}
Salida:
20-4-2010
21-4-2010
22-4-2010
-1
Prueba de esto, espero que ayuda ayuda
$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;
}
Run Like: date_from = 20/10/2015, date_to = 2016-04-15
$arr_weeks = $this->getWeeksBetweenTowDates($date_from, $date_to);
Salida:
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
)
Cuestiones relacionadas
- 1. encontrar objetos entre dos fechas MongoDB
- 2. Imprimir todas las fechas-día entre dos fechas
- 3. Matriz de fechas entre dos fechas
- 4. ¿Cómo puedo encontrar el número de años entre dos fechas?
- 5. ¿Cuántos segundos entre dos fechas?
- 6. ¿Cómo puedo encontrar la diferencia entre dos fechas usando Javascript
- 7. ¿Cómo encontrar el número de días entre dos fechas
- 8. clojureql seleccionar entre dos fechas
- 9. SQL Seleccionar entre las fechas
- 10. C# - ¿Diferencia entre dos fechas?
- 11. Consulta SQL para seleccionar fechas entre dos fechas
- 12. genarate fechas entre dos fecha en android
- 13. Número de viernes entre dos fechas
- 14. Cómo encontrar la diferencia entre las fechas en VBA
- 15. ¿Cómo obtener todas las fechas (mes, día y año) entre dos fechas en python?
- 16. calcular la diferencia entre dos fechas
- 17. Seleccione entre dos fechas con Django
- 18. Cómo comparar dos fechas
- 19. Diferencia entre fechas en dos filas consecutivas
- 20. Enumere los meses entre dos fechas
- 21. Días, horas, minutos, segundos entre dos fechas
- 22. Android diferencia entre dos fechas en segundos
- 23. Contabilizar días de trabajo entre dos fechas
- 24. Diferencia entre dos fechas en MySQL
- 25. codeigniter: Obteniendo datos publicados entre dos fechas
- 26. bash loop entre dos fechas dadas
- 27. Filtrar datos entre dos fechas utilizando LINQ
- 28. Crear una matriz o lista de todas las fechas entre dos fechas
- 29. ActionScript 3.0 + ¿Calcula el intervalo de tiempo entre dos fechas?
- 30. Días entre dos fechas en Python
+1 The Bestest Reply ... –
+1 para las clases Date *, -1 por llamarlo "de lujo" y "overkill", +1 por dar algo diferente de la solución 'strtotime' a menudo citada. :) – salathe
@gordon ¿Puedes explicar por qué establecer las 10:00 a.m. hora allí permite que los usuarios den cuenta del horario de verano adecuadamente? Para referencia, por qué lo pido, consulte: http://stackoverflow.com/questions/15302469/problems-with-finding-the-days-between-two-unix-timestamps – Shackrock