Treating bank search dates

Asked

Viewed 111 times

1

I’m performing the following capture of a date:

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

To search the bank precisely leave it in the standard yyyy/mm/dd:

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

But if conducted the research of the day more than today does not work, for example: 25/12/2014 returns "1969-31-12"

And another detail is that I need to do a search between a date and another, I know of the use of between in sql, but using ZF1 I do not know well,

 $select = $this->select();
        $select->where("data_prazo = ?", $dtInicial);
        $select->where("data_prazo = ?", $dtFinal);
  • Just to say that the code to search between the dates worked like this: $select->Where("data_prazo between '$dtInicial' and '$dtFinal'"); ;)

  • I don’t know the purpose, but already thought about using dates with format UNIX_TIMESTAMP

2 answers

2


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

Php Manual In this phpmanual comment here it is suggested that you use . to deal with dates in ISO format(Y.m.d), however, if you wish, you can also use -, because our date format is European(d-m-Y). Both the . as to the - 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))); 

1

I recommend using the class Datetime php;

$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!
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.