Filtering Dates in PHP

1

I'm trying to filter dates, the issue is like this:

By method POST I get a date with the format "yyy-mm-dd" plus the time with format "HH:MM:SS" , the date and time are separate variables of type string.

$fecha = "2016-08-29";
$hora = "13:00:00";

With this date and time I want to make a filtering that fits the following requirement:

  

"Show a form only on days Monday to Friday from 07:00 a.m. to 10:00 p.m. and Saturdays from 10:00 a.m. to 2:00 p.m.".

For example: The variable $fecha and $hora conform to the requirement, so it should show some information, a form.

I am dealing with some methods of PHP as date() , time() , however I can not solve the problem.

If you had any idea or recommendation, I would appreciate it very much.

    
asked by Iras 01.09.2016 в 21:45
source

1 answer

2

Given the strings

$fecha = "2016-08-29";
$hora = "13:00:00";

A first algorithm that complies with this sentence:

  

"Show a form only on days Monday to Friday from 07:00 to 22:00   hrs and Saturdays from 10:00 a.m. to 2:00 p.m. ".

We could be:

//************************************
// horarioComercial(fecha,hora)
//
// Devuelve verdadero si la fecha y la
// hora dadas se encuentran en horario 
// comercial
//************************************

function horarioComercial($fecha,$hora)
{
  //Convertimos los strings en fechas unix
  $fecha_unix = strtotime($fecha);
  $hora_unix = strtotime($hora);

  $dia = date("w",$fecha_unix); // Día de la semana en formato númerico
  $hora = date("H",$hora_unix); // Hora en formato 00

  if($dia > 0) //Es entre lunes y sábado
  {
    if($dia > 0 && $dia < 6) //Si es de lunes a viernes
    {
      if( $hora >= 7 && $hora <= 22 ) // Y es de 7:00 a 22:00
      {
        return true; // Horario comercial
      }
      else
      {
        return false;
      }
    }
    else if($dia == 6) // Es sábado
    {
      if( $hora >= 10 && $hora <= 14) // Entre las 10 y las 14
      {
        return true; // Horario comercial
      }
      else
      {
        return false;
      }
    }
    else
    {
      return false;
    }
  }
}

It should work ... then the form you implement as you see. If you are programming after a web server, you remove the arguments from the command line and replace them with the get or post variables or how you send them.

I hope I have understood you.

    
answered by 01.09.2016 / 23:28
source