Match a date to a time date format in php - php

I am trying to get records from my database which are added on a specific date, for example records added on the 2013-09-05. However the records are stored in my database in datetime format so 2013-09-06 08:22:35
I have tried to use a like format thinking it might just match the first half but that makes no difference.
SELECT search_term, COUNT(search_term) as count FROM $tableName WHERE client_id = '{$client_id}' AND timedate LIKE '$dates' ORDER BY count DESC LIMIT $start, $limit
I would be grateful for suggestions or a nudge in the right direction.
Thanks

you could use DATE() to get date part from datetime, so change:
...AND timedate LIKE '$dates'...
to
...AND DATE(timedate) = '$dates'...
or you could also do:
...AND timedate LIKE '$dates%'...

You can convert date using DATE_FORMAT
Use in Query as below,
AND DATE_FORMAT(timedate, '%Y-%m-%d' ) = '$dates'
Document

Related

MYSQL Query - Sort by Date in PHP

I'm trying to set up a query that will search a MYSQL database and only pull in the rows from the database who's expiry_date is after todays date.
I would also like to be able to work out how many days or weeks there are remaining from todays date to the expiry date of the rows in the database that match the above query.
I think that in order to get the current date I would have to set up a variable of $date = time(); which I will then later be able to use to compare against the expiry_date column in the database. However I am now stumped as what to do to achieve the required result. I'm not exactly a PHP noob but I'm not an expert either, so please go easy on me ;)
Thanks in advance!
If the Column you want to check is a DATE(TIME), try
$sql="SELECT column FROM table WHERE expiry_date > CURDATE()";
If you saved the UNIX timestamp, you can simply use
$sql="SELECT column FROM table WHERE expiry_date > '".time()."'";
If you use the first with "NOW()" or the second, you'll proably get results for the current day.
If this is not acceptable, try "mktime(0, 0, 0)" instead of time();
Use this query
$query = "select timestampdiff(days,'$exipry_date','$now')";

Wrong date from mysql in order by

Greetings hope to get some help from you here as i been searching high and low for this.
This query works but its not the results i wanted more correctly not the right date format i want it in.
SELECT DISTINCT colum FROM table WHERE colum IS NOT NULL
This query gives me the dates
01.04.13
02.04.13
03.04.13
30.03.13
31.03.13
I wanted it to show latest date.
This info is posted in colum that stores the info as text. It is posted in the following format
dd.mm.yy
I wanted it then to show me the results as in
03.04.13 since this is todays date. i know i can limit it to 1 but still it will show the wrong date.
Thank you again for all help so far
First, you should not be storing a date as a string, you should store a data as a DateTime datatype.
Since you are storing it as a string, you will have to convert it to a date to get the max() date value. The following uses the STR_TO_DATE() function to convert the string to a date to get the max value:
select max(str_to_date(yourdate, '%d.%m.%y')) MaxDate
from table1
See SQL Fiddle with Demo
Try
select distinct column from tab where column1 = date_format(curdate(),'%d.%m.%y') AND brukernavn is not null order by brukernavn;
1) You need to save DATES in date or datetime
2) try MAX() function in MySQL
or try to ORDER BY date DESC and LIMIT 1
3) by the way, did you try using DISTINCT with GROUP BY?

Search mysql database for date range

I have a database where users enter a date, among other things, with a drop down calendar, the date format is in this format 21-NOV-2012 .
Is there a way to search the database with a date range ?
I currently search date using the following ::
sql ="SELECT * FROM ircb WHERE date LIKE '%$term1%' AND date LIKE '%$term2%' LIMIT $start_from, 15";
term1 is month and term 2 is year , this does not allow for date range within the month only for the month.
any help appreciated, thanks
I know this doesn't really solve your problem but I guess it's a testing (and you have access to the database) and will risk myself of getting flamed by telling you a better approach, as I will feel guilty if don't tell you what would be the correct way to do it:
You should have a Date type field in your database (I assume mysql) and store the whole date in that field, and mysql can search using that date (normally the standard date format would be YYYY-MM-DD) .
You just need to do:
SELECT * FROM TABLE WHERE date > 'your date';
or ........................... < 'your date';
or .................WHERE date BETWEEN 'date1' AND 'date2';
It will allow you to do those kind of operations in a much easier and human-readable way.
Also have a look at the datetime if you are interested, as you can do the same but with the time of that day also included in the same field! :D
Have a good look at the field types, as it's essential to have the database healthy and optimized.
date >= '$term2-$term1-01' AND date < DATE_ADD( '$term2-$term1-01', INTERVAL 1 MONTH)
I think this should work.
sql ="SELECT * FROM ircb WHERE date BETWEEN '$term1' AND '$term2' LIMIT $start_from, 15";
BETWEEN will let you fetch the range date, and you can delete those LIKE.

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.
Why %$date? You should do the opposite.
WHERE date LIKE "".$date."%"
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.
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'
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';
this is what worked for me
$date = "2018-05-13";
"SELECT * FROM $username WHERE reg_date LIKE '%$date%'";

MYSQL most recent time

Is there a way to grab the most recent time of day. If your data in the database is formatted like this 07:00AM and 08:00pm and 12:00pm. Sorta like max(). But for the time. In a Mysql query.
Thanks
Eric
It would be best to store it in another format rather than as text. Or at least store it in 24 hour format, then a simple sort would work. You can convert it to 12-hour format when you display the data to the user.
But assuming you can't change your database schema, try this:
SELECT *
FROM your_table
ORDER BY STR_TO_DATE(your_time, '%h:%i%p') DESC
LIMIT 1
Note that this won't be able to use an index to perform the sorting.
You should try STR_TO_DATE() instead if you're using a string. If your times are always formatted as hh:mmAMPM, you can use:
MAX(STR_TO_DATE(YourTimeField,'%h:%i%p'))
This converts your string to a time, without any need to split it up by substring or anything, so MySQL would then see 09:07AM as 09:07:00 and 02:35PM as 14:35:00, and then would easily be able to determine the MAX of it.
Assuming you are dealing with a DATETIME field in your MySQL, you can use this query to get the max time per day:
SELECT DATE(YourDateField), MAX(TIME(YourDateField)) FROM YourTable
GROUP BY DATE(YourDateField)
When you are dealing with a VARCHAR field, you can try a hack like this:
SELECT YourDateField, SUBSTRING(MAX(
CASE WHEN YourTimeField LIKE '%AM%' THEN '0' ELSE '1' END
+ REPLACE(YourTimeField, '12:', '00:')
), 2)
GROUP BY YourDateField
You can just sort.
select time_column from table order by time_column desc limit 1;

Categories