¿Cómo puedo calcular el número de sábados y domingos entre dos fechas en php?Cálculo del número de sábados y domingos
¿Hay alguna función incorporada para tal fin?
¿Cómo puedo calcular el número de sábados y domingos entre dos fechas en php?Cálculo del número de sábados y domingos
¿Hay alguna función incorporada para tal fin?
Hay una pregunta relacionada aquí ya, Calculate business days
Usted puede usar esto para restar de 7 para obtener los fines de semana o similares.
No creo que hay una construida en para eso, pero esto debe hacer el trabajo:
$startTime = START_TIMESTAMP;
$endTime = END_TIMESTAMP;
$time = $startTime;
$count = 0;
while(date('w', $time) != 0) { // 0 (for Sunday) through 6 (for Saturday)
$time += 86400;
}
while($time < $endTime) {
$count++;
$time += 7 * 86400;
}
qué pasa si timestamp en startdate llega el sábado – nik
Funciona también, 'date ('w', $ time) == 0' así que ve al segundo while y' $ count ++ ' , tienes un sábado al menos. –
En lugar de 'while (date ('w', $ time)! = 0)' puedes hacer '$ time = strtotime (" Saturday ")' or '$ time = strtotime (" Sunday ")'. –
<?php
date_default_timezone_set("Europe/Lisbon");
$d1 = new DateTime("2009-06-01"); /* inclusive */
$d2 = new DateTime("2009-07-01"); /* exclusive */
$interval = $d2->diff($d1);
$number_of_days = $interval->format("%d");
$number_of_weekends = $number_of_days/7;
$remainder = $number_of_days % 7;
if ($remainder >=2 && $d1->format("D") == "Sat")
$number_of_weekends++;
elseif ($d1->format("w") + $remainder >= 8)
$number_of_weekends++;
Puede haber perdido por uno en la última condición, asegúrese de consultarlo con diferentes fechas de inicio. (No dude en editar esta respuesta si detecta un error).
definitivamente no hay construida en función de eso, pero se pueden utilizar strtotime al bucle días
$start = strtotime('2010-01-01');
$end = strtotime('2010-01-09');
function numWeekdays($start_ts, $end_ts, $day, $include_start_end = false) {
$day = strtolower($day);
$current_ts = $start_ts;
// loop next $day until timestamp past $end_ts
while($current_ts < $end_ts) {
if(($current_ts = strtotime('next '.$day, $current_ts)) < $end_ts) {
$days++;
}
}
// include start/end days
if ($include_start_end) {
if (strtolower(date('l', $start_ts)) == $day) {
$days++;
}
if (strtolower(date('l', $end_ts)) == $day) {
$days++;
}
}
return (int)$days;
}
echo numWeekDays($start, $end, 'saturday', false);
nos dejaron todo de KISS (Keep It Simple Stupid). ¿Por qué hacerlo tan complicado?
function countWeekendDays($start, $end)
{
// $start in timestamp
// $end in timestamp
$iter = 24*60*60; // whole day in seconds
$count = 0; // keep a count of Sats & Suns
for($i = $start; $i <= $end; $i=$i+$iter)
{
if(Date('D',$i) == 'Sat' || Date('D',$i) == 'Sun')
{
$count++;
}
}
return $count;
}
Busqué un tiempo para una solución simple que funcionó y decidió escribir mi propia y se acercó con esta
$start = date('Y-m-d');
$end = date('Y-m-d', strtotime($start.' +1 year'));
$current = $start;
$count = 0;
while($current != $end){
if(date('l', strtotime($current)) == 'Saturday'){
$count++;
}
$current = date('Y-m-d', strtotime($current.' +1 day'));
};
echo $count;
posible duplicado de [Calcular días hábiles] (http://stackoverflow.com/questions/336127/calculate-business-days) –