miércoles, 10 de agosto de 2011

PHP: Cálculo de semana laboral actual

Dejo a disposición un código que permite obtener la semana laboral actual (lunes a viernes tomando domingo como inicio de semana).

Salida del código:

From: 08-08-2011 To: 08-12-2011


<?php


class DateUtils {

public static function get_current_date() {
return substr(DateUtils::get_current_date_time(), 0, 10);
}

public static function get_current_date_time() {
return date('Y-m-d H:i:s');
}

public static function get_day_of_current_week_as_number() {
return date("w",mktime());
}

public static function get_day_of_week_as_number($date_as_string) {
return date("w", strtotime($date_as_string));
}

public static function substract_days_to_current_date($days) {
$operation = "-";

if ($days < 0) {
$operation = "+";
$days = abs($days);
}
$newdate = strtotime ( $operation. $days .' day' ,strtotime ( date('Y-m-d') )) ;
return date ( 'm-d-Y' , $newdate );
}

public static function substract_days_to_date($date_as_string, $days) {

$operation = "-";

if ($days < 0) {
$operation = "+";
$days = abs($days);
}
$newdate = strtotime ( $operation . $days .' day' , strtotime ( $date_as_string) ) ;
$newdate = date ( 'm-d-Y' , $newdate );
return $newdate;
}

public static function get_start_day_of_current_work_week() {
$monday_delta = 0;
$current_day_of_week = DateUtils::get_day_of_current_week_as_number();

if($current_day_of_week > 1) {
$monday_delta = $current_day_of_week-1;
} else if($current_day_of_week == 0){
$monday_delta = -1;
}
return DateUtils::substract_days_to_current_date($monday_delta);
}

public static function get_start_day_of_work_week($date_as_string) {
$monday_delta = 0;
$day_of_week = DateUtils::get_day_of_week_as_number($date_as_string);

if($day_of_week > 1) {
$monday_delta = $day_of_week-1;
} else if($day_of_week == 0){
$monday_delta = -1;
}
return DateUtils::substract_days_to_date($date_as_string, $monday_delta);
}

public static function get_delta_for_end_day_of_work_week($current_day_of_week) {
$friday_delta = 0;

switch ($current_day_of_week) {
case 0: // Sunday.
$friday_delta=-5;
break;
case 1: // Monday.
$friday_delta=-4;
break;
case 2: // Tuesday.
$friday_delta=-3;
break;
case 3: // Wednesday.
$friday_delta=-2;
break;
case 4: // Thursday.
$friday_delta=-1;
break;
case 5: // Friday.
$friday_delta=0;
break;
case 6: // Saturday.
$friday_delta=1;
break;
}
return $friday_delta;
}

public static function get_end_day_of_current_work_week() {
$friday_delta = 0;
$current_day_of_week = DateUtils::get_day_of_current_week_as_number();

return DateUtils::
substract_days_to_current_date(DateUtils::
get_delta_for_end_day_of_work_week($current_day_of_week ));
}

public static function get_end_day_of_work_week($date_as_string) {
$friday_delta = 0;
$current_day_of_week = DateUtils::get_day_of_week_as_number($date_as_string);

return DateUtils::
substract_days_to_date($date_as_string, DateUtils::
get_delta_for_end_day_of_work_week($current_day_of_week ));
}

public static function get_current_work_week_range() {
return "From: " . DateUtils::get_start_day_of_current_work_week() . " To: "
. DateUtils::get_end_day_of_current_work_week();
}

}
echo "<h1>" . DateUtils::get_current_work_week_range() . "</h1>";
?>


No hay comentarios:

Publicar un comentario