thegrede
thegrede

Reputation: 494

Search date in database PHP

When I was a start up student in PHP I made my database to store dates the date and time together, now I have a big problem, I have already in the database over 3000 orders but when I want to make a search with dates am I in big trouble because the dates and time is together in one field, I tried to make the query like where date LIKE '%$date' but I'm getting no results, has anybody any idea what I can do now?

And also how can I change the whole database it should be all dates and time separately and it should not effect my database?

UPDATE:

The data in the database looks like, 10/16/2012 5:00pm

Appreciate any help.

Upvotes: 2

Views: 22776

Answers (5)

EMMANUEL AJAERO
EMMANUEL AJAERO

Reputation: 66

this is what worked for me

$date = "2018-05-13";

"SELECT * FROM $username WHERE reg_date LIKE '%$date%'";

Upvotes: 0

Will
Will

Reputation: 24699

If it's a DATETIME field, you can use

WHERE DATE(datetime_field) = '01-01-2012';

or (better, as it can use indexes)

WHERE datetime_field >= '01-01-2012 00:00:00' AND datetime_field <= '01-01-2012 23:59:59';

Upvotes: 1

Zulakis
Zulakis

Reputation: 8384

You can do the following:

$date = "2012-03-08";
$sql = "SELECT * FROM table WHERE date => '$date 00:00:00' AND date =< '$date 23:59:59'

Edit: Seeing your edit, this does not work anymore. You will need to convert your date column to a proper MySQL datetime or TIMESTAMP type.

At the current database design you could use something like this:

date

$date = "10/16/2012";
$sql = "SELECT * FROM table WHERE date LIKE '$date%'

time

$time = "5:00pm";
$sql = "SELECT * FROM table WHERE date LIKE '%$time'

Upvotes: 2

Andrew Kozak
Andrew Kozak

Reputation: 1660

In response to the sections of your question:

1. Finding the dates you need in the current schema.

Based on your edits, use:

 <?php

 $query = "SELECT * FROM table_name WHERE `date` LIKE '{$date}%'";

 ?> 

A query similar to what you posted should help you:

<?php

$query = "SELECT * FROM table_name WHERE `date` LIKE '%{$date}%'";

?>

Please note that your use of % in your question ( '%$date' ) will only match values that end with $date, while the pattern in my example ( '%{$date}%' ) will match values that have $date anywhere in them. Alternatively, you could use '{$date}%' to match date at the beginning of the value -- not sure which you want.

2. Updating your schema to split date and time into two columns.

The first step you should take here, is to add two columns ( date_only and time_only ) to your table. Next, update your code to process and store this information in addition to the 'all-in-one' date column your are currently using; you don't want to break your current codebase by switching over in one step. Once you can verify that date/time data is being written the way you want it to be, the third step is to read (and log) from the new date/time columns along with your production reads to date. Once you can verify that the reads are working as planned, switch over your dev environment to read from the new columns and test until you are confident that everything works.

Upvotes: 2

HBv6
HBv6

Reputation: 3537

Why %$date? You should do the opposite.

WHERE date LIKE "".$date."%"

Upvotes: 5

Related Questions