Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Bru_ce

Selecionar parte de uma string no Banco

Recommended Posts

Ele tem um input cuja data é digitada em 00/00/0000

echo date('Y-m-d', strtotime('21/03/2013'));
http://codepad.viper-7.com/gzoZRd

o no seu banco de dados ele está nesse formato: 0000-00-00 00:00:00 entenderam o problema ? não adianta ele comprar pois vai dar errado por causa do horário

mysql> select DATE('2013-03-21') = TIMESTAMP('2013-03-21');
+----------------------------------------------+
| DATE('2013-03-21') = TIMESTAMP('2013-03-21') |
+----------------------------------------------+
|                                            1 |
+----------------------------------------------+
1 row in set (0,00 sec)
CREATE TABLE `teste` (
  `data` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `teste` VALUES ('2013-03-21 14:04:54'),('2013-01-01 21:44:30'),('2013-12-31 08:20:35'),('2012-04-19 22:04:55');
mysql> select * from teste;
+---------------------+
| data                |
+---------------------+
| 2013-03-21 14:04:54 |
| 2013-01-01 21:44:30 |
| 2013-12-31 08:20:35 |
| 2012-04-19 22:04:55 |
+---------------------+
4 rows in set (0,00 sec)
mysql> select * from teste where data between '2013-01-01' and '2013-12-31';
+---------------------+
| data                |
+---------------------+
| 2013-03-21 14:04:54 |
| 2013-01-01 21:44:30 |
+---------------------+
2 rows in set (0,00 sec)
Veja que a linha #3 ficou de fora pelo horário ser [inline]08:20:35[/inline]. Se alterarmos para meia-noite em ponto:
mysql> update teste set data = '2013-12-31 00:00:00' where data = '2013-12-31 08:20:35';
Query OK, 1 row affected (0,05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from teste where data between '2013-01-01' and '2013-12-31';
+---------------------+
| data                |
+---------------------+
| 2013-03-21 14:04:54 |
| 2013-01-01 21:44:30 |
| 2013-12-31 00:00:00 |
+---------------------+
3 rows in set (0,00 sec)

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.