2010-01-11 8 views
7

que necesito para dar salida a una lista de fechas (sólo los lunes y martes) para los próximos 12 meses desde la fecha actual, así:Conseguir todas las fechas de los lunes y martes para el próximo año

Ene 2010
mar 12 Ene 2010
lun 18 Ene 2010
Mar 19 Ene 2010
lun 25 Ene 2010
Feb 2010
mar 02 Feb 2010
lun 08 Feb 2010
mar 09 Feb 2010
lun 15 Feb 2010
mar 16 Feb 2010
lun 22 Feb 2010
Mar 2010
mar 09 Mar 2010
lun 15 Mar 2010
mar 16 Mar 2010
. ..

Al ser nuevo en PHP, califiqué strtotime y el bucle en las siguientes 52 semanas es la mejor manera de ir

$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates 
$blockedDates = explode ("," , $blockedDatesInput); // convert to array 
$currentMonth = ""; // current month marker 

// loop over the next 52 weeks to find Mondays and Tuesdays 
for($i=0; $i<=52; $i++){ 
// build the month header 
$monthReference = date("M Y", strtotime('+'.$i.' Week')); 

// check if date exists in $blockeddate 
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) || 
    !in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates)) { 
    // check if we have to show a new month 
    if(strcmp($monthReference, $currentMonth) <> 0){ 
     echo $monthReference.'<br />',"\n"; 
    }else{ 
     // output the dates 
     echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n"; 
     echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n"; 
    } 
     $currentMonth = date("M Y", strtotime('+'.$i.' Week')); 
    } 
} 

Sin embargo la salida de mi código es

Ene 2010
lun 18 Ene 2010
mar 12 Ene 2010
lun 25 Ene 2010
Mar 19 Ene 2010
Feb 2010
lun 08 feb 2010
mar 02 feb 2010
lun 15 Feb 2010
mar 09 Feb 2010
lun 22 Feb 2010
mar 16 Feb 2010
Mar 2010
lun 08 Mar 2010
mar 02 Mar 2010
lun 15 Mar 2010
mar 09 Mar 2010
lun 22 Mar 2010
mar 16 Mar 2010
lun 29 Mar 2010
mar 23 Mar 2010

Como puede ver, las fechas no están en el orden correcto y me siento perdido cuando me equivoco aquí.

¿Hay una manera más elegante/simple de resolver esto?

versión de PHP es utilizado 5.2.11 y no hay posibilidad de ir a un 5,3 en el corto plazo :-(

Gracias por su ayuda.

código de abajo modificación según lo sugerido por Aly. Se cambió la fecha de la computadora del martes, 01/12/2010 al miércoles, 13/01/2010 para probar la salida.

$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates 
$blockedDates = explode ("," , $blockedDatesInput); // convert to array 
$currentMonth = ""; // current month marker 

// loop over the next 52 weeks to find Mondays and Tuesdays 
for($i=0; $i<=52; $i++){ 
// build the month header 
$monthReference = date("M Y", strtotime('+'.$i.' Week')); 

// check if date exists in $blockeddate 
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) || 
    !in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates)) { 
    // check if we have to show a new month 
    if(strcmp($monthReference, $currentMonth) <> 0){ 
     echo $monthReference.'<br />',"\n"; 
    }else{ 
     // output the dates (changed the order as suggested by Aly) 
     echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n"; 
     echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";   
    } 
     $currentMonth = date("M Y", strtotime('+'.$i.' Week')); 
    } 
} 

Salida de nuevo en el orden incorrecto.

Ene 2010
Mar 19 Ene 2010
lun 18 Ene 2010
Mar 26 Ene 2010
lun 25 Ene 2010
Feb 2010
mar 09 Feb 2010
lun 08 Feb 2010
mar. 16 feb 2010
lun 15 feb 2010
mar 23 feb 2010
lun 22 feb 2010

+0

vea mi respuesta editada – Aly

Respuesta

10

Esta es una interesante. Así es como yo lo haría con las funciones, aunque puede justificar su propia clase a ser muy modular y reutilizable:

Set up my date formats and excluded dates 
define('INTERNAL_FORMAT', 'Y-m-d'); 
define('DISPLAY_MONTH_FORMAT', 'M Y'); 
define('DISPLAY_DAY_FORMAT', 'D d M Y'); 
// format excluded dates as YYYY-MM-DD, date('Y-m-d'): 
$excluded_dates = array(
    '2010-03-09', 
    '2010-04-13', 
); 

entonces necesito algunas funciones de utilidad para ver cómo funcionan las fechas, y lo que las fechas están excluidos:

// date('w') returns a string numeral as follows: 
// '0' Sunday 
// '1' Monday 
// '2' Tuesday 
// '3' Wednesday 
// '4' Thursday 
// '5' Friday 
// '6' Saturday 
function isTuesday($date) { 
    return date('w', strtotime($date)) === '2'; 
} 
function isWednesday($date) { 
    return date('w', strtotime($date)) === '3'; 
} 

// handle the excluded dates 
function isExcludedDate($internal_date) { 
    global $excluded_dates; 
    return in_array($internal_date, $excluded_dates); 
} 

Ahora sólo tenemos que iterar sobre todos los días de la próxima 365 (el próximo año) y comprobar para ver si son martes o miércoles y no en la lista de excluidos. Almacenamos esto en $months_and_dates:

$start_date = date(INTERNAL_FORMAT); 

// something to store months and days 
$months_and_dates = array(); 

// loop over 365 days and look for tuesdays or wednesdays not in the excluded list 
foreach(range(0,365) as $day) { 
    $internal_date = date(INTERNAL_FORMAT, strtotime("{$start_date} + {$day} days")); 
    $this_day = date(DISPLAY_DAY_FORMAT, strtotime($internal_date)); 
    $this_month = date(DISPLAY_MONTH_FORMAT, strtotime($internal_date)); 
    if ((isTuesday($internal_date) || isWednesday($internal_date)) 
     && !isExcludedDate($internal_date)) { 
      $months_and_dates[$this_month][] = $this_day; 
    } 
} 

Usted puede print_r() ella, o para obtener la pantalla que desea, hacemos esto:

foreach($months_and_dates as $month => $days) { 
    print $month . "<br>"; 
    print implode('<br>', $days); 
    print "<br>"; 
} 

Aquí está el resultado a partir de hoy, 11 de enero de 2010:

Jan 2010 
Tue 12 Jan 2010 
Wed 13 Jan 2010 
Tue 19 Jan 2010 
Wed 20 Jan 2010 
Tue 26 Jan 2010 
Wed 27 Jan 2010 
Feb 2010 
Tue 02 Feb 2010 
Wed 03 Feb 2010 
Tue 09 Feb 2010 
Wed 10 Feb 2010 
Tue 16 Feb 2010 
Wed 17 Feb 2010 
Tue 23 Feb 2010 
Wed 24 Feb 2010 
Mar 2010 
Tue 02 Mar 2010 
Wed 03 Mar 2010 
Wed 10 Mar 2010 
Tue 16 Mar 2010 
Wed 17 Mar 2010 
Tue 23 Mar 2010 
Wed 24 Mar 2010 
Tue 30 Mar 2010 
Wed 31 Mar 2010 
Apr 2010 
Tue 06 Apr 2010 
Wed 07 Apr 2010 
Wed 14 Apr 2010 
Tue 20 Apr 2010 
Wed 21 Apr 2010 
Tue 27 Apr 2010 
Wed 28 Apr 2010 
May 2010 
Tue 04 May 2010 
Wed 05 May 2010 
Tue 11 May 2010 
Wed 12 May 2010 
Tue 18 May 2010 
Wed 19 May 2010 
Tue 25 May 2010 
Wed 26 May 2010 
Jun 2010 
Tue 01 Jun 2010 
Wed 02 Jun 2010 
Tue 08 Jun 2010 
Wed 09 Jun 2010 
Tue 15 Jun 2010 
Wed 16 Jun 2010 
Tue 22 Jun 2010 
Wed 23 Jun 2010 
Tue 29 Jun 2010 
Wed 30 Jun 2010 
Jul 2010 
Tue 06 Jul 2010 
Wed 07 Jul 2010 
Tue 13 Jul 2010 
Wed 14 Jul 2010 
Tue 20 Jul 2010 
Wed 21 Jul 2010 
Tue 27 Jul 2010 
Wed 28 Jul 2010 
Aug 2010 
Tue 03 Aug 2010 
Wed 04 Aug 2010 
Tue 10 Aug 2010 
Wed 11 Aug 2010 
Tue 17 Aug 2010 
Wed 18 Aug 2010 
Tue 24 Aug 2010 
Wed 25 Aug 2010 
Tue 31 Aug 2010 
Sep 2010 
Wed 01 Sep 2010 
Tue 07 Sep 2010 
Wed 08 Sep 2010 
Tue 14 Sep 2010 
Wed 15 Sep 2010 
Tue 21 Sep 2010 
Wed 22 Sep 2010 
Tue 28 Sep 2010 
Wed 29 Sep 2010 
Oct 2010 
Tue 05 Oct 2010 
Wed 06 Oct 2010 
Tue 12 Oct 2010 
Wed 13 Oct 2010 
Tue 19 Oct 2010 
Wed 20 Oct 2010 
Tue 26 Oct 2010 
Wed 27 Oct 2010 
Nov 2010 
Tue 02 Nov 2010 
Wed 03 Nov 2010 
Tue 09 Nov 2010 
Wed 10 Nov 2010 
Tue 16 Nov 2010 
Wed 17 Nov 2010 
Tue 23 Nov 2010 
Wed 24 Nov 2010 
Tue 30 Nov 2010 
Dec 2010 
Wed 01 Dec 2010 
Tue 07 Dec 2010 
Wed 08 Dec 2010 
Tue 14 Dec 2010 
Wed 15 Dec 2010 
Tue 21 Dec 2010 
Wed 22 Dec 2010 
Tue 28 Dec 2010 
Wed 29 Dec 2010 
Jan 2011 
Tue 04 Jan 2011 
Wed 05 Jan 2011 
Tue 11 Jan 2011 
+0

artlung gracias por su ayuda. Un poco más elaborado que el que tenía en mente. Objetivo que tiene sentido. En Java-Me he resuelto a través de un mapa hash también. Este material php es muy extraño para mí :-) Gracias de nuevo – interrogativus

+0

gran respuesta, muchas gracias! – Bear

+0

¿No hay una solución con Javascript? –

0

en una coincidencia graciosa, porque hoy es lunes, se salta un valor de lunes, por eso parece que no funciona. ayer hubiera funcionado bien.

es decir, su "+ 0 lunes" es PRÓXIMO lunes, no hoy.

es posible que desee buscar en el carácter de formato "N" para la fecha().

+0

realidad en mi cuello de los bosques que ya es el martes :-) embargo, la secuencia de las fechas es incorrecto. Creo que es lo que estoy preguntando. – interrogativus

0

Ok ahora que la fecha de su computadora es el miércoles, usted desea imprimir los lunes antes de los martes, ya que el próximo lunes es más cercano al miércoles que el próximo martes. Así que intente esto:

$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates 
$blockedDates = explode ("," , $blockedDatesInput); // convert to array 
$currentMonth = ""; // current month marker 

// loop over the next 52 weeks to find Mondays and Tuesdays 
for($i=0; $i<=52; $i++){ 
// build the month header 
$monthReference = date("M Y", strtotime('+'.$i.' Week')); 

// check if date exists in $blockeddate 
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) || 
    !in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates)) { 
    // check if we have to show a new month 
    if(strcmp($monthReference, $currentMonth) <> 0){ 
     echo $monthReference.'<br />',"\n"; 
    }else{ 
     // output the dates (changed the order as suggested by Aly) 
     echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";   
     echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n"; 
    } 
     $currentMonth = date("M Y", strtotime('+'.$i.' Week')); 
    } 
} 
+0

He cambiado la secuencia tal como sugirió y luego cambié la fecha de mi PC al día de mañana (13 de enero de 2010) y la salida volvió a ser incorrecta. Me falta algo fundamental aquí, supongo.

de enero de 2010
mar 19 de enero de 2010
mis 18 de enero de 2010
mar 26 de enero de 2010
mis 25 de enero de 2010
Feb 2010
mar 09 Feb 2010
MY 08 Feb 2010
mar 16 Feb 2010
Mi hijo de 15 Feb 2010
mar 23 Feb 2010
lun 22 Feb 2010

interrogativus

+0

¿Por qué cambió su fecha de aussi PC realice una o la otra - se puede editar el post con el nuevo código y la hora actual de su PC está mostrando – Aly

Cuestiones relacionadas