PHP fecha - encontrar el primer lunes de mes

  • jokerper
  • Novice
  • Novice
  • Avatar de Usuario
  • Registrado: Jun 20, 2011
  • Mensajes: 19
  • Status: Offline

Nota Enero 14th, 2012, 5:54 pm

Hola,


¿Cómo encontrar en este

PHP fecha - encontrar el primer lunes de mes.

como hoy 14.1.2012 que gané para encontrar el primer lunes de febrero ahora y después este lunes quiero encontrar la siguiente en marzo y así sucesivamente excepto de junio.

¿cualquier ayuda, por favor?
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Enero 14th, 2012, 5:54 pm

  • WritingBadCode
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Abr 28, 2011
  • Mensajes: 214
  • Loc: Sweden
  • Status: Offline

Nota Enero 19th, 2012, 10:01 am

Si no me equivoco fechas puede suponer que tendrá un lunes pasando cada 7 días. :D Y también puede safley asumir que sólo tendrá un lunes cada 7 días.

De todos modos aquí es un código que le imprime si el lunes por primera vez este mes:

Código: [ Select ]
if ( date("D") === "Mon" && (date("j")) < 8 )
{
    echo "Today is the the first Monday this month!";
}
  1. if ( date("D") === "Mon" && (date("j")) < 8 )
  2. {
  3.     echo "Today is the the first Monday this month!";
  4. }


El código anterior sólo comprobará el día de hoy y si es su el primer lunes de este mes, a continuación, se activan y decir que hoy en día... bla bla bla. No se pide para eso.

De todos modos aquí es un código que escribí a toda prisa:

Código: [ Select ]

$day = 1;
$month = 1;
$yearNow = 2011; //start counting from this year
$countToYear = 2020; // stop when reaching this year
while ($yearNow < $countToYear)
{
    if ( $month == 6 ) //skip June (for some reason?)
    {
        $month++;
        continue;
    }
    if ( date("l", mktime(0, 0, 0, $month, $day, $yearNow)) == "Monday" ) // prints out IF monday
    {
        echo date("Y-m-d", mktime(0, 0, 0, $month, $day, $yearNow));
        echo "<br />";
    }
    $day++;
    if ($day > 7) /* there are more than 7 days in a month but the first monday will happen in the first 7 days, so start over. with new month please */
    {
        $day = 1;
        $month++;
        if($month > 12)
        {
            $month = 1;
            $yearNow++;
        }
    }
}
  1. $day = 1;
  2. $month = 1;
  3. $yearNow = 2011; //start counting from this year
  4. $countToYear = 2020; // stop when reaching this year
  5. while ($yearNow < $countToYear)
  6. {
  7.     if ( $month == 6 ) //skip June (for some reason?)
  8.     {
  9.         $month++;
  10.         continue;
  11.     }
  12.     if ( date("l", mktime(0, 0, 0, $month, $day, $yearNow)) == "Monday" ) // prints out IF monday
  13.     {
  14.         echo date("Y-m-d", mktime(0, 0, 0, $month, $day, $yearNow));
  15.         echo "<br />";
  16.     }
  17.     $day++;
  18.     if ($day > 7) /* there are more than 7 days in a month but the first monday will happen in the first 7 days, so start over. with new month please */
  19.     {
  20.         $day = 1;
  21.         $month++;
  22.         if($month > 12)
  23.         {
  24.             $month = 1;
  25.             $yearNow++;
  26.         }
  27.     }
  28. }


Su no bastante, pero debe dar una lista mirando como:

2011-01-03
2011-02-07
2011-03-07
2011-04-04
2011-05-02
2011-07-04
2011-08-01
2011-09-05
2011-10-03
07-11-2011
2011-12-05
2012-01-02
06-02-2012
05-03-2012
02-04-2012
07-05-2012
02-07-2012
06-08-2012
03-09-2012
2012-10-01
2012-11-05
2012-12-03
07-01-2013
04-02-2013
04-03-2013
2013-04-01
2013-05-06
etc.

Se trata de la primera vez que sucede un lunes de cada mes. Como se puede ver 20XX-06-XX se omite porque no desea calcular cuando sucede en junio. Esto se hace mediante el uso de un articulo de continuar:

Código: [ Select ]
  if ( $month == 6 )
  {
    $month++;
    continue;
  }
  1.   if ( $month == 6 )
  2.   {
  3.     $month++;
  4.     continue;
  5.   }


Puede comprobar las fechas más en el tiempo o volver atrás en el tiempo más alterando $yearNow y $countToYears inicial de valores iniciales. Yo dejaría el día $ = 1; y $mes = 1; valores de ser como es, sólo dicen que se inicie el primer "analizar" con 1 mes y 1 día.
  • jokerper
  • Novice
  • Novice
  • Avatar de Usuario
  • Registrado: Jun 20, 2011
  • Mensajes: 19
  • Status: Offline

Nota Enero 20th, 2012, 9:07 am

Hola,

Primero gracias por su respuesta.

Puedo verlo trabajar mucho.

¿Me puede decir dónde cambiar si gané el 2. o 3.
4. o 5.

porque voy a actualizar algunos eventos en el futuro.

Así que puedo usar para ponerlo en un rato e insertar en una base de datos.

todos los scheudle(wrong) en una base de datos.

Gracias de antemano.

/jokerper

Publicar Información

  • Total de mensajes en este tema: 3 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 175 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC