Mysql SELECT by date format as 24 hour - php

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.

Related

Convert time() from SQL for monthly sales PHP

I want to make monthly sales from the sales table but i store date data using time() format
Example :
UserID | Product | Date
1 | Hot Dog | 1504363230
4 | Chicken | 1504695631
1 | Potato | 1504761716
3 | Spinach | 1505003789
So, how can i create monthly sales from there without replace Date into date() ? Because it will take a long time if i have to change 300K row
What should i do ?
Select * FROM Sales WHERE UserID = UserID AND Date = ?
the output must be like this
UserID 1 do 2 transaction in the last month or UserID 1 do 3 transaction in this
month
A PHP solution would be to use the second parameter in the date function:
string date ( string $format [, int $timestamp = time() ] )
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
You can convert a unix timestamp to a datetime string in MySQL with FROM_UNIXTIME.
SELECT FROM_UNIXTIME(`Date`) as `Datetime` FROM sales
You can then expand on this with other date functions like MONTH or YEAR
SELECT MONTH(FROM_UNIXTIME(`Date`)) as `Month` FROM sales
EDIT:
I think this should work for you. I guess you want to avoid PHP's date() but MySQL's DATE() is okay.
SELECT
COUNT(*) AS nb_transaction,
UserID
FROM
sales
WHERE
DATE(`Date`) LIKE '2018-06-%'
Of course replace the month with the one you are looking for.
SQL (will be faster than PHP):
You can convert any UNIX Timestamp using FROM_UNIXTIME() to any format:
FROM_UNIXTIME(`Date`, '%Y %D %M %h:%i:%s')
PHP:
The time() function returns a UNIX Timestamp.
If you want to display a date using one, you can use DateTime::setTimestamp:
$date = new DateTime();
$date->setTimestamp($row['Date']);
echo $date->format('Y-m-d H:i:s'); // Hot Dog = 2017-09-02 14:40:30
Or shortly:
echo (new DateTime())->setTimestamp($row['Date'])->format('Y-m-d H:i:s');
$timestamp = 1517317337107; $date=date('Y-m-d H:i:s',
$timestamp/1000);
echo "date and time : ". $date."<br>";
$month=date('Y-m-d',$timestamp/1000);
echo "only date : ".$month."<br>";
#output
date and time : 2018-01-30 13:02:17
only date : 2018-01-30
hope this will help

select varchar as date in mySQL

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.

How to ordinal form date using DATE_FORMAT()?

My Mysql query
select DATE_FORMAT(max(lastmodified), '%d %M %y , %r') from client_log
and my actuall output is
i want result 15th june 16, 02:17:09PM, i have to apply this ordinal concept in php logic but here i don't know how to perform in date_format() function or is there any function available for mysql in timestamp filed?
The issue is %d and it should be %D
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
select DATE_FORMAT(NOW(), '%D %M %y , %r') ;
+-------------------------------------+
| DATE_FORMAT(NOW(), '%D %M %y , %r') |
+-------------------------------------+
| 27th June 16 , 02:57:00 PM |
+-------------------------------------+
1 row in set (0.04 sec)
Try this Format.
select DATE_FORMAT(max(lastmodified), '%D %M %y , %r') from client_log

TIME in mysql minus 1 hour

i am selecting the time from a DATETIME field in mysql as a valid RFC2822 dateformat.
But i want mysql to select the date but directly subtract 1 hour of the time.
This is my script now:
$sql_select_bezig = "SELECT
DATE_FORMAT(verzonden_op,'%a, %d %b %Y %T') AS rfc_date,
message_id
FROM
email_tracking
WHERE
DATE(verzonden_op) > DATE_SUB(now(), INTERVAL 2 WEEK)";
Now it is only selecting the date and displays it like Sun, 14 Sep 2014 20:13:33 +0100.
That's ok, but how do i subtract 1 hour?
Select DATE_FORMAT(Date_SUB(date, 2, HOURS)...) AS ... from ...
In a SQL statement, we can substitute an expression for a column name.
Anywhere column name verzonden_op appears in your statement, you can replace that with the expression that VMai suggested...
verzonden_op - INTERVAL 1 HOUR

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