I am having a problem in mysql query, I have date saved in database in this format :
Wed, 26 Oct 2011 01:25:35 EDT
I want to sort the rows by date but this date format is not letting me do it. I have tried Date_FORMAT AND STR_TO_DATE function but couldn't get it working, could you please help me solve this?
Assuming you've modified your tables so you've got the original date field ('olddate') and want to put the reformatted 'native' date into a new field ('newdate'), then:
UPDATE yourtable
SET newdate = STR_TO_DATE(olddate, '%a, %e %b %Y %H:%i:%s')
however, MySQL does not store timezone information in its date/time fields, so you'd have to convert the EDT stuff to whatever TZ is desired (UTC?).
Once converted to native format, you'd do your sorting with:
SELECT ...
...
ORDER BY YEAR(newdate)
This format is just plain text to MySQL and you should have another one as datetime or timestamp with the same data but something that MySQL could understand.
add a new column
use PHP to strtotime your actual row and replicate that value to the new column.
…
profit!
Related
I have varchar field in database and format of that field is like '10 Feb 2016 08:26 PM'.
Now I want to fetch upcoming data, So how can i get that ?
$ctime=strtotime(date("Y-m-d H:i:s"));
if($type=='1'){
$books = $objData->getAll("select *
from bookings
where custID='".$custID."'
and fromTime>'".$ctime."'");
}
But I am not getting correct result, please help.
First of all, as mentioned in comments you should use proper types for dates. To answer your question, it is still possible to achieve, using STR_TO_DATE mysql function.
$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());
Link: Convert VARCHAR timestamp to TIMESTAMP?
PHP Mysql i want to convert date format from mysql... please help me.
Like
SELECT * FROM agentcommission where AgentId='AC-1363717254' AND Date between STR_TO_DATE('24-Mar-2013','%d-%M-%Y') and R_TOST_DATE('31-Apr-2013','%d-%M-%Y');
but it shows Zero rows.
Like
18-Apr-2013 is my date format and i want to convert it to 18-04-2013 Means string to date format
you can use DATE_FORMAT function to convert date from one form to another.
select DATE_FORMAT(STR_TO_DATE('24-Mar-2013','%d-%M-%Y'),'%d-%m-%Y');
This statement will give you the required formatted date as 24-03-2013.
But as said above you need a YYYY-MM-DD format to use the BETWEEN operator to compare dates in your custom date format.
You need %b not %M
%b Abbreviated month name (Jan..Dec)
SELECT STR_TO_DATE('24-Mar-2013','%d-%b-%Y')
Returns
March, 24 2013 00:00:00+0000
If your date column is a date type then you need a YYYY-MM-DD format. You can't use the BETWEEN operator to compare dates in your custom date format.
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 use NOW() function but I get this weird date:
2011-11-06
How do I get the following European date:
ss:mm:hh dd:mm:year (06-11-2011)
In my database I set the field column date as DATE
How do you integrate DATEFORMAT into this query:
$avatar_Q=mysql_query("
SELECT user_name,avatar,reputation,comment, DATE_FORMAT(date,'%d/%m/%Y %h:%i') AS rightNow
FROM comments AS com
INNER JOIN users AS us ON com.user_id=us.user_id
WHERE comment_id=$commentID
") or die(mysql_error());
The date is is in the column table
MySQL uses the following date format - YYYY-MM-DD - if you want a different format you need to use the DATE_FORMAT function on select
for example :
SELECT DATE_FORMAT(datecolumn,"%d/%m/%Y %h:%i")
FROM atable
To integrate the date_format function into your select statement you need to list the fields individually
Yep, use a Timestamp column and insert with CURRENT_TIMESTAMP. It saves a lot of time! :)
My personal recommendation is to save the date as a UNIX Timestamp (using the time() function) ,
This way you could format it as you wish .
Shai.
DATE_FORMAT
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
That format is just how MySQL stores the data type DATE: http://dev.mysql.com/doc/refman/5.5/en/datetime.html.
If you're using the DATE data type for you column you can either:
Use the DATE_FORMAT function in your SQL to get the date in your desired format
Use a combination of PHP's strtotime and date functions to display your date in the most appropriate format
I have a column that is populated with a date format that is useful for another application but not the mysql format for timestamps. Here is what it looks like:
2010.01.28 12:00 ("time" column) instead of the mysql timestamp: 2010-01-28 12:00:00 ("updatetime" column).
I need to do a date specific search between two time periods and for a normal mysql timestamp I would run this:
SELECT * FROM table WHERE updatetime BETWEEN '$dateStart' and '$dateEnd'
but this doesn't work with the "time" column formatted as it is. I would prefer to keep that column formatted as such as a different application requires that date format, so does anyone know a MYSQL way to search BETWEEN two timeperiods with the 2010.01.28 12:00 format? Running from a PHP script
You can either format the times in the same format in PHP, or in MySQL
WHERE updatetime BETWEEN
date_format('$datestart','%Y.%m.%d %H:%s')
AND date_format('$dateend','%Y.%m.%d %H:%s')
You might want to try something like this
SELECT * FROM table WHERE STR_TO_DATE(updatetime, "%d.%m.%Y %h:%i")
BETWEEN '$dateStart' and '$dateEnd'
to convert the string into a date - that is if the values of $dateStart and $dateEnd are already in the correct format for comparison with a sql date