Código PHP:
<html>
<head>
<script language="JavaScript">
// Funciones comunes a todos los métodos de selección
function padNmb(nStr, nLen, sChr){
var sRes = String(nStr);
for (var i = 0; i < nLen - String(nStr).length; i++)
sRes = sChr + sRes;
return sRes;
}
function makeDateFormat(nDay, nMonth, nYear){
var sRes;
sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "0");
return sRes;
}
function prevMonth(nMonth){
return ((nMonth + 10) % 12) + 1;
}
function prevMonth_Year(nMonth, nYear){
return nYear - (((nMonth + 10) % 12) + 1 == 12? 1: 0);
}
function lastDayOfMonth(nMonth, nYear){
var aMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if ((nMonth == 2) && (nYear % 4 == 0))
return 29;
else
return aMonth[nMonth - 1];
}
function incDate(sFec0){
var nDia = parseInt(sFec0.substr(0, 2), 10);
var nMes = parseInt(sFec0.substr(3, 2), 10);
var nAno = parseInt(sFec0.substr(6, 4), 10);
nDia += 1;
if (nDia > lastDayOfMonth(nMes, nAno)){
nDia = 1;
nMes += 1;
if (nMes == 13){
nMes = 1;
nAno += 1;
}
}
return makeDateFormat(nDia, nMes, nAno);
}
function decDate(sFec0){
var nDia = parseInt(sFec0.substr(0, 2), 10);
var nMes = parseInt(sFec0.substr(3, 2), 10);
var nAno = parseInt(sFec0.substr(6, 4), 10);
nDia -= 1;
if (nDia == 0){
nMes -= 1;
if (nMes == 0){
nMes = 12;
nAno -= 1;
}
nDia = lastDayOfMonth(nMes, nAno);
}
return makeDateFormat(nDia, nMes, nAno);
}
function addToDate(sFec0, nInc){
var nIncAbs = Math.abs(nInc);
var sRes = sFec0;
if (nInc >= 0)
for (var i = 0; i < nIncAbs; i++) sRes = incDate(sRes);
else
for (var i = 0; i < nIncAbs; i++) sRes = decDate(sRes);
return sRes;
}
// Funciones individuales de cada método de selección
// Esta semana completa (Lun -> Dom)
function setDefaults1(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var nDOW = dToday.getDay();
var sFirstDate, sLastDate;
if (nDOW == 0) nDOW = 7;
sFirstDate = addToDate(makeDateFormat(nDay, nMonth, nYear), -1 * (nDOW - 1));
sLastDate = addToDate(sFirstDate, 6);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Lo que va de esta semana (Lun -> Hoy)
function setDefaults2(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var nDOW = dToday.getDay();
var sFirstDate, sLastDate;
if (nDOW == 0) nDOW = 7;
sLastDate = makeDateFormat(nDay, nMonth, nYear);
sFirstDate = addToDate(sLastDate, -1 * (nDOW - 1));
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// La semana pasada (Lun -> Dom)
function setDefaults3(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var nDOW = dToday.getDay();
var sFirstDate, sLastDate;
if (nDOW == 0) nDOW = 7;
sLastDate = addToDate(makeDateFormat(nDay, nMonth, nYear), -1 * nDOW);
sFirstDate = addToDate(sLastDate, -6);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Los últimos siete días
function setDefaults4(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sLastDate = makeDateFormat(nDay, nMonth, nYear);
sFirstDate = addToDate(sLastDate, -6);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Este mes completo
function setDefaults5(oFrom, oTo){
var dToday = new Date();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sFirstDate = makeDateFormat(1, nMonth, nYear);
sLastDate = makeDateFormat(lastDayOfMonth(nMonth, nYear), nMonth, nYear);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Lo que va de este mes
function setDefaults6(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sFirstDate = makeDateFormat(1, nMonth, nYear);
sLastDate = makeDateFormat(nDay, nMonth, nYear);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// El mes pasado
function setDefaults7(oFrom, oTo){
var dToday = new Date();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
nYear = prevMonth_Year(nMonth, nYear);
nMonth = prevMonth(nMonth);
sFirstDate = makeDateFormat(1, nMonth, nYear);
sLastDate = makeDateFormat(lastDayOfMonth(nMonth, nYear), nMonth, nYear);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Los últimos treinta días
function setDefaults8(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sLastDate = makeDateFormat(nDay, nMonth, nYear);
sFirstDate = addToDate(sLastDate, -29);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Este año completo
function setDefaults9(oFrom, oTo){
var dToday = new Date();
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sFirstDate = makeDateFormat(1, 1, nYear);
sLastDate = makeDateFormat(31, 12, nYear);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Lo que va de este año
function setDefaults10(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sFirstDate = makeDateFormat(1, 1, nYear);
sLastDate = makeDateFormat(nDay, nMonth, nYear);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// El año pasado
function setDefaults11(oFrom, oTo){
var dToday = new Date();
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sFirstDate = makeDateFormat(1, 1, nYear - 1);
sLastDate = makeDateFormat(31, 12, nYear - 1);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
// Los últimos 365 días
function setDefaults12(oFrom, oTo){
var dToday = new Date();
var nDay = dToday.getDate();
var nMonth = dToday.getMonth() + 1;
var nYear = dToday.getYear();
var sFirstDate, sLastDate;
sLastDate = makeDateFormat(nDay, nMonth, nYear);
sFirstDate = addToDate(sLastDate, -364);
oFrom.value = sFirstDate;
oTo.value = sLastDate;
}
function setDefaults(oFrom, oTo, nMeth){
switch (nMeth){
case 0:
alert("");
break;
case 1:
setDefaults1(oFrom, oTo);
break;
case 2:
setDefaults2(oFrom, oTo);
break;
case 3:
setDefaults3(oFrom, oTo);
break;
case 4:
setDefaults4(oFrom, oTo);
break;
case 5:
setDefaults5(oFrom, oTo)
break;
case 6:
setDefaults6(oFrom, oTo)
break;
case 7:
setDefaults7(oFrom, oTo)
break;
case 8:
setDefaults8(oFrom, oTo)
break;
case 9:
setDefaults9(oFrom, oTo)
break;
case 10:
setDefaults10(oFrom, oTo)
break;
case 11:
setDefaults11(oFrom, oTo)
break;
case 12:
setDefaults12(oFrom, oTo)
break;
}
}
</script>
</head>
<body>
<form name="formulario">
<table border="0">
<tr>
<td colspan="2" style="font-family: helvetica; text-align: center; font-size: 12; font-weight: bold">
VALORES DE INICIO DE UN RANGO DE FECHAS
</td>
</tr>
<tr height="50">
<td align="right">
Tipo de rango:
</td>
<td>
<select name="sel" onchange="setDefaults(document.formulario.fecha_desde, document.formulario.fecha_hasta, this.selectedIndex)">
<option>...seleccione</option>
<option>Esta semana completa</option>
<option>Lo que va de esta semana</option>
<option>La semana pasada</option>
<option>Los últimos siete días</option>
<option>Este mes completo</option>
<option>Lo que va de este mes</option>
<option>El mes pasado</option>
<option>Los últimos treinta días</option>
<option>Este año completo</option>
<option>Lo que va de este año</option>
<option>El año pasado</option>
<option>Los últimos 365 días</option>
</select>
</td>
</tr>
<tr>
<td align="right">
Desde (dd/mm/yyyy):
</td>
<td>
<input type="text" name="fecha_desde">
</td>
</tr>
<tr>
<td>
Hasta (dd/mm/yyyy):
</td>
<td>
<input type="text" name="fecha_hasta">
</td>
</tr>
</table>
</form>
</body>
</html>