Handling dates for searching the bank

1

I am performing the following capture of a date:

$dtInicial  = $this->_getParam('dataInicial'); // Padrão dd/mm/aaaa

To search the precise database, leave it in the yyyy / mm / dd pattern:

$dt1 = date('Y-m-d', strtotime($dtInicial)); 

But if you did the research of the day that does not work today, for example: 25/12/2014 returns "1969-31-12"

And another detail is that I need to do a search between one date and another, I know how to use between in sql, but using ZF1 I'm not sure,

 $select = $this->select();
        $select->where("data_prazo = ?", $dtInicial);
        $select->where("data_prazo = ?", $dtFinal);
    
asked by anonymous 12.12.2014 / 14:57

2 answers

2

The problem is that the date() function is m/d/Y (American date format), so when you pass 25/12/2014 , the 25th day is actually considered as 25th month, as 25th does not exist the function returns this default date.

Php Manual In this comment here from phpmanual it is suggested that you use . to treat on dates in ISO (Ymd) format, however, if you wish, you can also use - , because our date format is European (dmY). Both . and - will solve your problem.

$dtInicial  = $this->_getParam('dataInicial');

$dt1 = date('Y-m-d', strtotime(str_replace("/",".",$dtInicial))); 
//Ou Formato Europeu
$dt1 = date('Y-m-d', strtotime(str_replace("/","-",$dtInicial))); 
    
12.12.2014 / 15:55
1

I recommend using the php DateTime class

$dtInicial  = $this->_getParam('dataInicial'); // Padrão dd/mm/aaaa
$objDate = DateTime::createFromFormat('d/m/Y', $dtInicial);

if ($objDate instanceof DateTime) {
  // Eh uma data
} else {
  // ops... nao eh uma data!
}
    
12.12.2014 / 18:14