select varchar as date in mySQL - php

I have a date column in the format
25 Mar 2017 07:19 pm
I want to select date in the format dd/mm/yyyy
I have tried: cast(date as DATE), STR_TO_DATE(date, '%d/%m/%Y') but everything returns null
any help will be appreciated.

Use
select str_to_date('25 Mar 2017 07:19 pm', '%d %M %Y')
The format specified has to match the input pattern. As the month name is included, use %M.

You can parse the date using following format:
SELECT STR_TO_DATE('25 Mar 2017 07:19 pm', '%d %M %Y %h:%i %p');
Here's the SQL Fiddle.

Related

Mysql SELECT by date format as 24 hour

I am trying to select data from a table mysql but I need to format a varchar date column in a military format, but cannot get my desire output my query is below.
"SELECT STR_TO_DATE(`hour`, '%Y-%m-%d %k:%i') AS `time` FROM `dataTable`"
When I tried to echo the time below format appear.
2017-09-21 00:00:00
My desire output is below which is in 24 hour format and removing the seconds, any suggestions would be great.
2017-09-21 18:00
Use the Date_format function of mysql.
"SELECT DATE_FORMAT(`hour`, '%Y-%m-%d %H:%i') AS `time` FROM `dataTable`"
Explanation:
The STR_TO_DATE function always return the date in the format of 2008-09-15 22:23:00
SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
+----------------------------------------------------------------------------+
| STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s') |
+----------------------------------------------------------------------------+
| 2008-09-15 22:23:00 |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
Check the above example: the second parameter in STR_TO_DATE function is the format of the parameter 1 to tell the function that in which format parameter 1 is passed in the function.
Reference taken from this link.

how can i get month and year from custom formated datetime in mysql?

I want to get month and year from this datetime format in form of number of month.
My datetime formate is 19 August 2015 - 10:50 am.
How can I get this?
I suppose you have a MySQL Varchar field that contains a custom string like '19 August 2015 - 10:50 am' that represents a date, you can get the date value with this:
SELECT str_to_date(customdate, '%c %M %Y - %h:%i %p') as date_column
FROM yourtable
Please see the str_to_date function documentation, and the list of available date formats.
Then you can just use MONTH and YEAR functions:
SELECT
YEAR(str_to_date(customdate, '%c %M %Y - %h:%i %p')) AS year,
MONTH(str_to_date(customdate, '%c %M %Y - %h:%i %p')) AS month
FROM
tablename
You can use date() and strtotime().
Will there always be a hyphen (-) in your date? Then, we have to remove it first by using str_replace().
$date = "19 August 2015 - 10:50 am"; /* YOUR GIVEN EXAMPLE */
$date = str_replace("-", "", $date); /* REMOVE HYPHEN */
$date = date("m Y", strtotime($date)); /* 08 2015 */
Us this
<?php
//date_default_timezone_get('Asia/Colombo');
$date = '19 August 2015 - 10:50 am';
echo date("Y/m", strtotime($date)); //2015/08
echo date("Y-m", strtotime($date)); // 2015-08
?>

How to select a php date() format in SQL

I have an SQL table with a field "time_created" in the form "Wed, 19 Aug 2015 03:58:00 -0600".
I need to check this field against today and perform a where statement according to it, as in
SELECT * FROM table WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_created) >= 3600*24*30
to select records where time_created is older then a month.
Problem is UNIX_TIMESTAMP doesn't understand formats as "Wed, 19 Aug 2015 03:58:00 -0600". Any other way?
Try:
SELECT *
FROM table
WHERE DATE(NOW()) > DATE_SUB(STR_TO_DATE(time_created, '%a, %e %b %Y %T'), INTERVAL 1 MONTH)
This will find all records where the current date (DATE(NOW())) is bigger than time_created subtract ` month.

INTERVAL 30 DAY not giving expected result

I am somewhat familiar with the MySQL DATE and DATE_FORMAT.
However, when running a query for a date range, using INTERVAL - 30 DAY, am either not getting the correct output or it throws an error. Looking further into the table, realize that each row for date_buy is in this type of format:
Mon 09 September 2015, etc. I have tried to use PHP date('D d F Y') instead of DATE(NOW()) and such, but am unable to resolve the issue. Is there another known workaround for this?
And yes, have tried a BETWEEN date_buy_x and date_buy_y. It returns nothing even though there are records in the table.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy >= $date - INTERVAL 30 DAY
GROUP BY date_buy
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy BETWEEN date_buy_x && date_buy_y
GROUP BY date_buy ORDER BY date_buy DESC
UPDATE
SO, based upon the suggestions, and looking further into the DATE_FORMAT and CURDATE parameters, have been able to display the data. The ORDER BY must also be in DATE_FORMAT(date_buy, '%a %d %M %Y') to output correctly. However, the issue still remains regarding INTERVAL 30 DAY parameter.
WHERE datebuy <= DATE_FORMAT(CURDATE() - INTERVAL 30 DAY,'%a %d %M %Y')
GROUP BY date_buy ORDER BY DATE_FORMAT(date_buy, '%a %d %M %Y') DESC
This is bringing back data but still way out of range, which makes me think it is not accepting the INTERVAL request.
Sat 22 November 2014 (50 sales)
Thu 18 December 2014 (50 sales)
Thu 22 January 2015 (20 sales)
Sun 25 January 2015 (20 sales)
Mon 06 April 2015 (25 sales)
Sun 12 April 2015 (25 sales)
Mon 03 August 2015 (10 sales)
When the output should only be:
Mon 03 August 2015 (10 sales)
You could conceivably do:
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= $date - INTERVAL 30 DAY
GROUP BY date_buy
Unless $date is also in this string format, in which case you need to use the STR_TO_DATE function on it too.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= DATE(STR_TO_DATE($date, '%a %d %M %Y')) - INTERVAL 30 DAY
GROUP BY date_buy
how about date_sub(),use date_format to same format when query
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE_FORMAT(DATE(date_buy,'%Y %m %d')) >= DATE_FORMAT(DATE_SUB(DATE($date,INTERVAL 30 DAY),'%Y %m %d'))
GROUP BY date_buy

Get formated date in php using mysql database

I want to get formated date in php from mysql database Like 21st March 2013 11:21AM
You can use MySQL's built-in function called DATE_FORMAT()
SELECT DATE_FORMAT(NOW(), '%D %M %Y %h:%i%p')
SQLFiddle Demo
DATE_FORMAT()
OUTPUT
╔═════════════════════════╗
║ FORMATTED_DATE ║
╠═════════════════════════╣
║ 22nd March 2013 06:07AM ║
╚═════════════════════════╝
You can use DATE_FORMAT MySQL function.
Example
DATE_FORMAT(NOW(),'DATEFORMATE') // DATEFORMATE can be %b %d %Y %h:%i %p
OR
STR_TO_DATE('$date', 'DATEFORMATE')
HOW TO USE IN QUERY
SELECT STR_TO_DATE('YOUR DATE FIELD','DATEFORMAT') FROM table;
SELECT DATE_FORMAT(NOW(),'DATEFORMAT') FROM table;
DATEFORMATE Options
Why not use MySQL's DATE_FORMAT?
For your example, add this to your MySQL query:
DATE_FORMAT(my_date, '%e %M %Y %h:%i %p')
Use DATE_FORMAT()
SELECT * FROM table DATA_FORMAT(NOW(), '%D %M %Y %h:%i%p')
Tutorial on DATE_FORMAT
What format is your database column in? If it's a UNIX timestamp, just feed it into date. If it's a date column (formatted as 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'), feed that into strtotime, then into date.
So, assuming it's a date column, you'd want to do something like this:
$column_value = "2013-03-01 00:00:00"; // Get this from your database however you like
$formatted_date = date("jS F Y H:i A", strtotime($column_value));
The documentation on how to use date for formatting is in the PHP documentation.
Try this :
$date = date from databse // some thing like 2013-03-22 11:38:00
$dt = new DateTime($date);
echo $dt->format('jS F Y g:ia');
Output : 22nd March 2013 11:38am
Ref : http://www.php.net/manual/en/datetime.format.php

Categories