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
Related
I have a column in MySQL datatable named added_date, datatype of it is varchar(255) which is inserting date like mm/dd/yyyy. I have a lot of data in the table for dates like '5/12/2018', '4/10/2018', '3/5/2018' etc. Now, I want to get data for may, 2018 month only.
How to have the data for may, 2018 only?
Thanks.
MySQL retrieves and displays dates in 'YYYY-MM-DD' format. Thus, you have to use STR_TO_DATE function to convert string to date. Then it is possible to use BETWEEN keyword in WHERE clause to set specific date range:
SELECT *
FROM MyTable
WHERE STR_TO_DATE(added_date, '%m/%d/%Y') BETWEEN '2018-05-01' AND '2018-05-31';
You could try the query online.
Convert varchar to date within a format that is suitable for your condition:
SELECT * FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';
You can use the same function in the select component to return the date too:
SELECT STR_TO_DATE(added_date, '%Y-%m-%d') AS fDate FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';
In mySQL I have a timestamp column named when
2015-01-07 16:43:21
My question is how using PHP/mySQL
For now I can show the results based on month number like
... where month(`when`) = '1' ...
but what if I want to show the rows of a particular date for example 2015-01-05 ?
I will pass the preferable date through a variable into the sql query.
Just wrap the date() function around your date string which is already in the YYYY-MM-DD format, and also around your timestamp field in the where clause:
select * from table where date(`when`) = date('2015-01-05');
From the documentation for date():
Extracts the date part of the date or datetime expression expr.
Here's how to convert the date via MySQL:
SELECT
DATE_FORMAT(`when`, '%Y-%m-%d') AS my_date
FROM
my_table
WHERE
MONTH(`when`) = 1;
Take a look at the MySQL documentation here for other formats: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_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 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')
I have a moodle installation in which there is a column in mdl_user table called firstaccess whose type is bigint(10) and contains date in following format 1266839570.
I am writing a query for accessing users according to date filters. For e.g. i want to check which users firstaccess is greater than '2010-04-12'. How can i convert the date? These two date formats are different. I think firstaccess is unix timestamp. Should i change the '2010-04-12' into unix timestamp or there is a way to convert firstaccess i.e 1266839570 to yyyy-mm-dd format.
Please help me on this.
Thanks
You can create a unix timestamp in php with the mktime() function, then simply put it in your query.
MySQL has a date_format() function, that can format dates however you like, but I'm not sure if it works with bigints. You'd better go with the mktime.
date() and mktime() are functions to concert from unix timestamp and back.
You can convert your dates in either way
I believe you can write your query using a timestamp. Eg.
SELECT * FROM mytable WHERE firstaccess >= TIMESTAMP('2010-04-12')
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I don't know what form the date in your form is, but you can easily convert it to a timestamp (if it already isn't one) using mktime. For example:
$mytimestamp=mktime(0,0,0, $month, $day, $year);
Then just add it to your query:
$myQuery= "SELECT whatever FROM sometable WHERE " . $mytimestamp . ">=firstaccess";
Like Paul Peelen, my answer is a MySQL query. I'm going the other way, though, and converting first access into a date.
Using the date information in your problem:
SELECT * FROM mytable WHERE DATE_FORMAT(FROM_UNIXTIME(firstaccess), '%Y-%m-%d') > '2010-04-12';