Sorting events by event date - how? - php

I have at table where I store all events. In that table I have a column named "event_date". The person who creates a event has to type the date like this:d/m/Y - example: 27/02/2013.
But when I try to sort the results by the event_date ASC, it doesn't come out in the desired order. I have even tried ordering it with UNIX_TIMESTAMP(event_date), but it doesn't solve my problem either.
How can I fix this?
There is one solution; when a person creates an event, I can convert it to a timestamp. But isn't it possible to sort it correctly as it is now?
Thanks in advance.

You would have to store your dates as YYYY/MM/DD in your VARCHAR column to have an alphanumeric sort work properly.
As it is, the system will sort:
27/02/2013
01/03/2013
06/03/2013
like this:
01/03/2013
06/03/2013
27/02/2013
If you can change your column to the DATE data type, you can either switch your input format to YYYY-MM-DD or continue to accept DD/MM/YYYY and use:
STR_TO_DATE('01/03/2013','%d/%m/%Y')
to convert the value to a valid date

Related

how to search records beetween two dates. But table date format is varchar

I have a table. In which, a column name is tb_date, which is varchar format. In that column dates are save, But in different-different format (Like: 01/07/201 OR 01-08-2018 or 2017/03/12 etc.).
Now I want a search between given date. But it is not working. I tried it-
SELECT * FROM `user_History` WHERE date_format(str_to_date(`tb_date`, '%d/%m/%Y'), '%d/%m/%Y') BETWEEN '01/06/2018' AND '31/06/2018'
But its giving all record.
I tried it in my sql.
Whats is the problem?
Problem is in your str_to_date(tb_date, '%d/%m/%Y'), you give a format %d/%m/%Y of importing date, but you have different formats in this field.
I think, you should process all your table by PHP and, for example, convert all your dates to UNIX_TIMESTAMP by function strtotime().
It will be more easy than try to create a SQL query for it.

EPOCH Unix MYSQL Query

I'am using a script that saves some fields into my database as Epoch & Unix Timestamp format, (I think because the field is submitting as something like 1515469971), the column is an "int" format type.
I want to know, how I can make a query, to search all the inputs that have created on the current month.
I am so confused about it, hope you can help me :)
Firstly, show me your table structure with show create table table_name.
If you're using an int to store the time. There are two ways to do that:
Convert the field to a date string with from_unixtime. Then you got a string like '2018-01-08 05:05:05', and just compare the month value.
Calculate the very beginning and the end of the current month in advance. Then just select * from table_name where time >= beginning_of_the_month and time <= end_of_the_month.

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Selecting records between two dates using PHP from MySql database

In my project , I am generating and storing the Bill (invoice).
The date of Bill is coming to the textbox from the javascript date picker(small pop-up calender) before saving.
The format of the date is : DD-MON-YYYY (18-JUN-2013).
I am using 'Text' data type for storing dates in MySql table.
I have done selecting of records(Previous Bills) from the table by given single date like. . .
$result = mysql_query("SELECT * FROM outward WHERE date='".$date."' ORDER BY billNo");
Now, what i want to do is:
To select records (Bills) between two dates.....
My exact Question is:
Is it possible to query mysql database with this settings or I have to make some changes to select records between 2 dates efficiently ?
How can i achieve this ?
P.s. - Is it effective to use
1. "SELECT * FROM outward WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "' ORDER by id DESC"
Or
2. SELECT * FROM outward WHERE date > "15-JUN-2013" and date < "18-JUN-2013"
You could do it in a pure SQL way, but you are going to have to do a full table scan for each query.
select the_dates,
STR_TO_DATE(the_dates, '%d-%M-%Y') as converted
from testing
where STR_TO_DATE(the_dates, '%d-%M-%Y') between '2013-06-20' and '2013-06-23'
Link to SQLFiddle
You should use strtotime PHP function to convert string date to UNIX timestamp format and change MySQL data type for date field to TIMESTAMP.
Than you can do effective queries with > and <.
If it's a DATE column, you can get all dates between 15 June 2013 and 18 June 2013 (inclusive) using this:
WHERE date BETWEEN '2013-06-15' AND '2013-06-18'
If it's a DATETIME column, do this instead:
WHERE date >= '2013-06-15' AND date < '2013-06-19'
If the date column is indexed, this approach will make sure the indexes are available for optimization. If it isn't indexed, the approach is just as fast as the many other ways you can do this.
Addendum: Just saw the "storing as text" amidst all the other shouted info. Note that this answer applies only if the type is DATE or DATETIME. I'll leave it up because the best answer is to change the column's data type and then use this or one of the other suggested options.
I am using 'Text' data type for storing dates in MySql table.
That's a problem. You should store dates as date or datetime data type in MySQL. If you don't care about the time part, date should be sufficient.
If you change your data type to date, then doing:
select x,y,z from table a where a.datecolumn between #startdate and #enddate
Should work fine.
If you use a text data type, you would have to cast the column to a date column and then apply your date selection range which is going to be slower due to the cast.
Always store data in the data type that matches its kind. If a date then a date column, if it's text then text or varchar, etc. The presentation layer of your app can worry about the format in which this data is presented to the user.
You said you were using a TEXT column to store the dates. That's an extremely bad idea. If you switch to a DATE or a DATETIME, then this becomes trivial.
Since you are storing it as text but you want SQL to parse it as a DATE SQL doesn't understand in the first place.
In your example SQL will use TEXT comparison rules. So 15-April < 15-Mar > 15-DEC
If you are storing dates in an SQL database you should be storing it as a Date and not as TEXT.

MySQL Date_Format based on today's date and another column?

I am aware of the MySQL Date_Format function but am looking to achieve the following:
I have on column with a day date in 2 digit format (01-30). I am trying to update another date formatted field with the current year, the next month (m+1) and the day field mentioned previously.
In PHP i would do this using mktime function but this must be done using mysql calls only.
Is it possible to transform in this way?
update table set field1 = concat(date_format(curdate(),"%Y-%m"),'-',field2) + interval 1 month
There is a function called STR_TO_DATE in mysql which you should be able to use to create a brand new date with using the seperate parts you described in your problem. The final input into the function should be STR_TO_DATE('01,5,2013','%d,%m,%Y') where argument one is the actual date string and argument two represents the format of the new date. You should be able to create the first argument by concatenating your parts together and then specifying whatever date format you need.

Categories