Foros del Web » Programando para Internet » PHP »

Fechas en PHP

Estas en el tema de Fechas en PHP en el foro de PHP en Foros del Web. Hola gracias por leer mi post. Ahora, lo que pasa es que estoy tratando de desarrollar un calendario semanal; o sea que me muestre algo ...
  #1 (permalink)  
Antiguo 31/05/2005, 15:51
 
Fecha de Ingreso: mayo-2005
Mensajes: 4
Antigüedad: 18 años, 10 meses
Puntos: 1
Exclamación Fechas en PHP

Hola gracias por leer mi post.

Ahora, lo que pasa es que estoy tratando de desarrollar un calendario semanal; o sea que me muestre algo asi por ejemplo:
__________________________________________________ ___________
| Domingo | Lunes | Martes |Miercoles| Jueves | Viernes | Sabado |
|29-05-05 |30-05-05 |31-05-05|01-06-05|02-06-05|03-06-05|04-06-05|
================================================== ======
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-----------------------------------------------------------------------

Y bueno mi problema empieza cuando obtengo el dia de hoy con su nombre, por ejemplo hoy es: lunes, 31 may 05.

Y como no se si exista alguna funcion para sumar con fechas, por ejemplo que:

31-05-2005 + 1 = 01-06-2005;
31-05-2005 + 2 = 02-06-2005;
31-05-2005 + 9 = 09-06-2005;

Por lo que me estoy matando haciendo este churrisimo en php del cual aun no termino, pero tengo la esperanza de que alguien me pueda ayudar diciendome que si existe alguna funcion que pueda yo usar para dicha tarea.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
extract($_POST);
$dias=$fecha;
$year =$dias[0].$dias[1].$dias[2].$dias[3];
$month =$dias[5].$dias[6];
$day =$dias[8].$dias[9];

if($month=='01' || $month=='03' || $month=='05' || $month=='07' || $month=='08' || $month=='10' || $month=='12')
{
switch($month)
{
case '01' : //En caso de que sea enero
$month_max=31;
$ant_month='12';
$act_month='01';
$next_month='02';
switch($dia)
{ //--------------------------------------------------------------------------------------------------|
case 'Domingo' : $domingo=$year.'-'.$month.'-'.$hoy; //En esta posicion nos encontramos situados trab.
$lunes=$year.'-'.$month.'-'.$hoy+1;
if($hoy+1>$month_max)
{
$hoy=1;
$month=$next_month;
$lunes=$year.'-'.$month.'-'.$hoy;
$hoy=0;
}
$martes=$year.'-'.$month.'-'.$hoy+2;
$miercoles=$year.'-'.$month.'-'.$hoy+3;
$jueves=$year.'-'.$month.'-'.$hoy+4;
$viernes=$year.'-'.$month.'-'.$hoy+5;
$sabado=$year.'-'.$month.'-'.$hoy+6;
break;
//--------------------------------------------------------------------------------------------------|
case 'Lunes' : $domingo=$year.'-'.$month.'-'.$hoy-1;
if($hoy-1<1)
{
$month=$ant_month;
$domingo=$year.'-'.$month.'-'.$month_max-1;
$hoy=1;
$month=$act_month;
}
$lunes=$year.'-'.$month.'-'.$hoy; //En esta posicion nos encontramos situados trab.
$martes=$year.'-'.$month.'-'.$hoy+1;
$miercoles=$year.'-'.$month.'-'.$hoy+2;
$jueves=$year.'-'.$month.'-'.$hoy+3;
$viernes=$year.'-'.$month.'-'.$hoy+4;
$sabado=$year.'-'.$month.'-'.$hoy+5;
break;
//--------------------------------------------------------------------------------------------------|
case 'Martes' : $domingo=$year.'-'.$month.'-'.$hoy-1;
if($domingo<1)
{
$domingo=31;
$hoy=1;
$month='0';
}
$lunes=$year.'-'.$month.'-'.$hoy;
$martes=$year.'-'.$month.'-'.$hoy+1; //En esta posicion nos encontramos situados trab.
$miercoles=$year.'-'.$month.'-'.$hoy+2;
$jueves=$year.'-'.$month.'-'.$hoy+3;
$viernes=$year.'-'.$month.'-'.$hoy+4;
$sabado=$year.'-'.$month.'-'.$hoy+5;
break;
//--------------------------------------------------------------------------------------------------|
case 'Miercoles' :

break;
//--------------------------------------------------------------------------------------------------|
case 'Jueves' :

break;
//--------------------------------------------------------------------------------------------------|
case 'Viernes' :

break;
//--------------------------------------------------------------------------------------------------|
case 'Sabado' :

break;
}
break;
case '03' : //caso de que sea marzo
if (((fmod($year,4)==0) and (fmod($year,100)!=0)) or (fmod($year,400)==0))
{
$month_max=29;
}
else
{
$month_max=28;
}
$ant_month='02';
$act_month='03';
$next_month='04';

break;
case '05' : //caso de que sea mayo
$month_max=30;
$ant_month='04';
$act_month='05';
$next_month='06';

break;
case '07' : //caso de que sea julio
$month_max=31;
$ant_month='06';
$act_month='07';
$next_month='08';

break;
case '10' : //caso de que sea octubre
$month_max=30;
$ant_month='09';
$act_month='10';
$next_month='11';

break;
case '12' : //caso de que sea diciembre
$month_max=30;
$ant_month='11';
$act_month='12';
$next_month='01';
break;
}
else if($month=='04' || $month=='06' || $month=='09' || $month=='11')
{
switch($month)
{
case '04' : //caso de que sea abril
$month_max=31;
$ant_month='03';
$act_month='04';
$next_month='05';

break;
case '06' : //caso de que sea junio
$month_max=31;
$ant_month='05';
$act_month='06';
$next_month='07';

break;
case '09' : //caso de que sea septiembre
$month_max=31;
$ant_month='08';
$act_month='09';
$next_month='10';

break;
case '11' : //caso de que sea noviembre
$month_max=31;
$ant_month='10';
$act_month='11';
$next_month='12';
break;
}
}
else
{
switch($month)
{
case '02' : //caso de que sea febrero
$month_max=31;
$ant_month='01';
$act_month='02';
$next_month='03';

break;
}
}



?>


</body>
</html>

Como pueden ver si termino la implemetacion para la primera semana osea de domingo a sabado de enero, con las variables que tengo por mes, solo copio el mismo codigo para las semanas de los meses subsecuentes y mi codigo correria(bueno ... eso espero; pero creo que la logica esta bien).

Espero que alguien me pueda ayudar.

Saludos, Vic!

Última edición por oxigen; 31/05/2005 a las 15:55 Razón: para mejorar el envio de respuestas de mi pregunta a mi mail
  #2 (permalink)  
Antiguo 31/05/2005, 15:58
Avatar de dwaks  
Fecha de Ingreso: agosto-2002
Ubicación: Panamá
Mensajes: 962
Antigüedad: 21 años, 7 meses
Puntos: 15
Solo hize de entrar a internet en google puse calendario php y salieron un monton de verdad esa herramienta ayuda mucho y deberia ser la primera opcion a buscar.

Aqui te dejo el codigo de lo que encontre.

Código PHP:
<?php

/*
Author: Vardhan Mudunuru (aka exacube)
Date: January 15th, 2005

Feel free to rip and dip this code ;)
*/

// $leap contains 1 if this is a new year. else, 0
$leap=date("l");

// Set $num_days as an array containing all the months and the number of days they have this year.
$num_days["January"]=31;

// If this is a leap year, February should have 29 days. else, 28
$num_days["February"]=($leap == 29 28);

$num_days["March"]=31;
$num_days["April"]=30;
$num_days["May"]=31;
$num_days["June"]=30;
$num_days["July"]=31;
$num_days["August"]=31;
$num_days["September"]=30;
$num_days["October"]=31;
$num_days["November"]=30;
$num_days["December"]=31;

// $days contains all the days in a week.. The use of this array will be examined later
$days=Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// Today's date in number (suchas 21th or whatever).
// $tnc will be used in a loop
$today_num $tnc date("j");
// Today's day of the week (eg: Sat)
$today_day=date("D");

// $tdc contains the index number of today in $days
// Why? you'll find out..
$tdc=array_search(date("D"),$days);

// This is loop is used to get the day of the week on the 1st day of this month (eg: January 1st)
// $tdc is the day of the week in number (the index number in $days)
// $tnc is decreased until it hits 1 (the first day of the month)
// $tdc is also decreased if it goes below 0 (sunday), then it is set to 6 (saturday)
while ($tnc 1) {
    
    
$tdc--;
    if (
$tdc 0) { $tdc=6; }
        
    
$tnc--;
    
}

// set $counter_day the first day of the month (eg: Saturday)
$counter_day=$days[$tdc];

// set $counter_day_num to 1 (the first day of the month... Goes all the way up to 31 depending on the month)
$counter_day_num=1;

// $total_days contains the number of days in this month.. that info is in the $num_days array
$total_days=$num_days[date("F")];

// $on is the column that we are in.. there are 7 columns: Sun, Mon, ... Sat
// when $on hits 6 (which is Sat), a new row is made (<tr></tr> html stuff), and we're on the first column (which is Sun)
// starts with 0, and not 1.. I like to follow the default standards
$on=0;

// Just the title.. displays the month and the year
$date_display_title="<b>"date("F")."</b> (<i><font size=1 color=green>".date("Y")."</font></i>)";

// Creates the table with 7 columns
echo "
    <table border=0 cellpadding=3 cellspacing=1>
    <tr><td colspan=7 class=calender_title>{$date_display_title}</td></tr>
    <tr>
    <td><b>S</b></td>
    <td><b>M</b></td>
    <td><b>T</b></td>
    <td><b>W</b></td>
    <td><b>T</b></td>
    <td><b>F</b></td>
    <td><b>S</b></td>
    </tr>
    <tr>
"
;

// This is the loop.. starts the the first day of the month and works its way up to the last day of the month
while ($counter_day_num <= $total_days) {
    
    
// create a new row if we're at the last column (which is Saturday)
    
if ($on 6) { $on=0; echo "</tr><tr>"; }
    
    
/* Jan 1st 2005 is on Saturday. We're on the Monday column,
        so leave this column blank, go to the next column, and start again from the loop's condition
    */
    
if ($counter_day != $days[$on]) { echo "<td>&nbsp;</td>"$on++; continue; }
    
    
/* if we are display a date thats not yet been past (which is the future), then dull down the colors
        For example, if its 16th today, and we are displaying the 17th, then set the text color to grey
    */
    
if (isset($dull)) {
        echo 
"<td style='background: rgb(220,220,220);'><font style='color: grey'>{$counter_day_num}</font></td>";
    }
    
    
/*
    If we are not displaying the future dates, then display it normally.
    If we are displaying today's date, set $dull as true so the dates that we dislay from here on are dull-colored
    */
    
else {
        echo 
"<td style='background: rgb(230,230,230);' align=center>"$counter_day_num ."</td>";
        if (
$counter_day_num == date("j")) { $dull=true; }
    }
    
    
// Move on to the next day, if all goes well
    
$counter_day_num++;
    
    
// Move on to the next day of the week's index (refer to the $days array)
    
$next_day=array_search($counter_day,$days) + 1;
    
    
// If its over 6 (saturday), then set it to 0 (sunday) since the 7th index doesn't exist in $days
    
$counter_day=$days[($next_day $next_day)];
    
    
// go to the next column
    
$on++;
}

// close the table..
echo "</tr></table>";

?>
  #3 (permalink)  
Antiguo 31/05/2005, 16:00
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años
Puntos: 17
Para generar una fecha es bastante más fácil (en PHP existen funciones para todo... cada día me asombro más)

Lee http://ar2.php.net/manual/es/function.mktime.php y las notas de abajo que tienen cosas útiles y creo q te facilitarían un toque el laburo
  #4 (permalink)  
Antiguo 31/05/2005, 16:24
 
Fecha de Ingreso: mayo-2005
Mensajes: 4
Antigüedad: 18 años, 10 meses
Puntos: 1
Gracias a los dos; pero continuo con algunas dudas.
Lo que pasa es que necesito que me despliegue SOLO lo de la semana actual, no importa que dia sea lunes martes... sabado domingo lo que necesito es que me muestre dicha semana de domingo a sabado.

Lo necesito, por que tengo que mostrar unos resultados que obtengo de mi base de datos. Por que estoy elaborando un sistemita que dice que unidades estan en transito semanal, pero solo lo de esta semana es lo que necesito, o lo que es lo de la semana actual.

Esto es para poder llevar una planeacion y control de trafico de las unidades que se encuentran de viaje, pero para ello necesito primero conocer los dias de la semana actual para poder con ellos buscar en la DB si existe algun viaje no importa si es salida o regreso a la planta con fecha entre inicio de semana a fin de semana y asi mostrarlos en un cuadro como el que puse arriba.

Si entienden mas o menos lo que quice decir de una manera muy enredada, les agradeceria mucho su apoyo.

Atte. Vic!
  #5 (permalink)  
Antiguo 31/05/2005, 18:58
Avatar de jam1138
/** @package Moderador */
 
Fecha de Ingreso: julio-2004
Ubicación: sèveR led onieR lE
Mensajes: 9.368
Antigüedad: 19 años, 7 meses
Puntos: 102
Cita:
Iniciado por oxigen
(...) Lo necesito, por que tengo que mostrar unos resultados que obtengo de mi base de datos. (...)
¿qué fué primero (el huevo ó la gallina )?? ... primero haces la consulta y luego muestras???.. ó primero muestras para poder hacer la consulta???

Para lo primero: http://www.forosdelweb.com/f21/mostrar-registro-semana-fechas-298774/
Para lo segundo: La misma lógica, solo hacer uso de la función date().. y quizá strtotime()... pero dudo sea este tu caso.

__________________
٩(͡๏̯͡๏)۶
» Cómo hacer preguntas de manera inteligente «

"100 años después, la revolución no es con armas, es intelectual y digital"
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:24.