Here my date values are stored in varchar and i want to get results while searching in between dates
Here is my code please have a look
public function get_report($from1,$to1)
{
$from=DATE($from1);
$to=DATE($to1);
$this->db->order_by('customer_account_id','desc');
$this->db->where('Date(`date`) >= ',$from);
$this->db->where('Date(`date`) <= ',$to);
return $this->db->get('customer_accounts');
}
my date_format is like this 31-01-2017 and when i use my code like this the result is not obtaining
MySQL date function expects dates in 'YYYY-MM-DD' format. If you change the date values that are stored in varchar column to be in that format then you should get the desired results. It will be better still if you convert the varchar column to be date as this will be faster when you select data from the table.
Use mysql CONVERT function to convert value from varchar to date while fetching.
Related
I have issue with this date m/d/y for example 01/09/15 but when i insert that in my table of field name date and declared date i adde to me 2001/09/15 how can i save this format 01/09/15 in field database of my table
You can't change the format of mysql dates. You could save your date as a varchar but you can't use any manipulation.
You can save it as a mysql date or a time int, and then cast it to the format that you want, using date function.
You can read more about this in this post:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
I hope this will be useful.
I have two tables, in both of them i have fields to store dates.
But in the first it stores the date as an integer and in the second like in time format.
I want to save the value from date format to integer, but it does not convert the date to int.
I tried the function idate(), but it converts only one char to parameter.
The date looks like string(19) "2006-08-09 13:22:44".
Can somebody help me?
Maybe strtotime() this my solution?
For PHP conversion use this:
$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);
And for pure MySQL conversion:
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Here, you can change the date string with your table field name (SELECT UNIX_TIMESTAMP(fieldName))
For reference:
https://stackoverflow.com/a/4577805/2883841 - the two answers with most votes.
Convert date from second table into Unix Time Stamp and save it into first table as int
EDIT
I think you should not do this conversion unless no other option left
Try UNIX_TIMESTAMP() e.g.
select UNIX_TIMESTAMP('2006-08-09 13:22:44')
1155126164
If your fieldtype is INT and you can't or don't want to change that to DATE or DATETIME, you should save the date as timestamp. In that case, strtotime is your function.
Instead of converting by php strtotime() you could convert the fields directly in mysql:
UPDATE table1 SET `dateInt`=UNIX_TIMESTAMP(table2.`dateDate`) WHERE table1.`id`=table2.`id`
Hope somebody can advise on the following:
I have created a table into my database which has a coloumn with datatype defined as DATE. The normal MySQL date order is YYYY-MM-DD, however I need to enter it in format DD-MM-YYYY.
I am using php to insert data into my table, and I was wondering at what point should I convert the date format.
To summerize, I am asking the following:
Is it possible to insert into MySQL database where field is formatted as DATE, a date in format DD-MM-YYYY instead in format YYYY-MM-DD?
If not possible, can I convert a value posted from PHP in format DD-MM-YYYY into the format YYYY-MM-DD using the MySQL syntax or I should use PHP to post the value in YYYY-MM-DD format?
Thank you in advance
You would use STR_TO_DATE in your INSERT to convert the date to the ANSI standard date format.
INSERT INTO `table` (`date`) VALUES (STR_TO_DATE('01-05-2013','%d-%m-%Y'));
You can then use DATE_FORMAT to convert the ANSI standard date to the format you want.
A date is always stored in the same manner in the DB.
When inserting you have to give the DB a format that it can parse as date. And when reading from the DB you can format the date in any way you like.
So you have to format your data before inserting into the correct format. Otherwise the DB can't store it as date.
You can go to different mysql website and get this answer. For Example:
http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.
Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.
Just use:
$date = date('d-m-Y', strtotime($dateFromDB));
That will convert from MySQL DateTime to the format you have specified.
It is possible to order by date fields, e.g.:
SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]
Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.
So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.
If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.
Store the date in default format that is yyyy-mm-dd
when you want to display in front end
use the following query to
select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;
For ordering by date
SELECT * FROM tbl
ORDER BY date DESC
I am working with a table on which I can't change the structure.... Now there is a varchar column which contains a timestamp. Now I need to select the records whose timestamp translates to the current date, or a specified date.
Any help?
First off you shouldn't be storing date information in a mysql database with a VARCHAR field. Rather use DATETIME that is what it is for. I can only guess how you have stored your timestamp date in the database but I am going to assume it is the following format:
YYYY-mm-dd hh:mi:ss (ie '2011-04-15 09:23:55')
You now have to format your input which I am assuming is a time object or it is a string in the same format as the data in the database:
$yourdate = strftime("%Y-%m-%d", $input);
then construct your query
query = "select * from table where substring(datecol, 1, 10) = '$yourdate'";
execute this and you should be good
Based on the format that you're storing the date as a string, use the STR_TO_DATE function to parse out the date. Then you can use it as a part of the where clause to query desired data.
try this
select * from table where date(your_field)=CURDATE()
or specific date
select * from table where date(your_field)=DATE_FORMAT(2011-05-31 00:02:00, '%Y-%m-%d')