PHP Get datetime from database without hours/minutes? - php

How can I get a datetime from database but without hours and minutes?
Format in database e.g 2016-03-28 15:55:00 I would like get this record if I type in SQL format y-m-d.

You would use date() in a where clause:
where date(col) = '2016-03-28'
Or, what is better for performance reasons (because the engine can use an index if available):
where date >= '2016-03-28' and
date < date_add('2016-03-28', interval 1 day)

You haven't mentioned the RDBMS you're using. I presume it must be either be MySQL or SQL Server.
MySQL:
Here's the docoumentation in the manual : DATE()
You can use it as such (illustration) :
SELECT DATE('2003-12-31 01:02:03')
Which will yield you : 2003-12-31
Hope this helps!!!
EDIT : I understand you want to apply filtering on it (i.e WHERE clause). This is one-way you can do it.
You can use this :
SELECT DATE(COLUMN_NAME)
FROM TABLE_NAME
WHERE DATE_FORMAT(COLUMN_NAME, '%Y %m %d') = DATE_FORMAT('2016-03-28', '%Y %m %d')
You can check it out on this fiddle here -> SQL Fiddle
Hope this helps!!!

You can use this SQL statement:
SELECT date(dateTime) From table

Related

how to convert a timestamp to 28 Dec, 2018 like format in php? [duplicate]

Can any one please let me know, i need to change the data format 2010-05-14 17:53 to 14/05/2010 17:53 using mysql select query
try this select..
SELECT DATE_FORMAT(datefield, "%d/%m/%Y %H:%i") FROM <TableName> WHERE <clause>
select date_format(date_column, "%d/%m/%Y %H:%i") from table_name
Maybe this link might help you
MySQL Manuel
This should do the trick :
Select DATE_FORMAT(now(),'%d/%m/%Y %H:%i');
For hour, u can use %H or %h, depending if you want 24h or 12h display.
%H will set 24h, %h will set 12h.
Use DATE_FORMAT to format your DATE. To solve your conversion, use the following code as a pointer
SELECT DATE_FORMAT('2010-05-14 17:53', '%d/%m/%Y %H:%i');

Changing date from query with PHP

I have a query (written to be easier from a class)
$cms->my_query('SELECT * FROM location');
Which will return an array
Though I have a DATE type in the mySQL Table which it is formatted like so 2014-06-22
Is there a way I can format so it's like this Nov 04 2008 11:45 PM with using DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p") now I believe DATE cannot use this properly so i'd have to use DATETIME but if that is the case it's fine but how do I select all and change date at the same time?
Example
$cms->my_query('SELECT * FROM location DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p")');
I just don't want that ugly 2014-06-22 and I have very little knowledge of mySQL and I am learning as I try new things out. So if someone who is more skilled please explain the best scenario for me, I'd like to learn and I am willing!
The first argument of DATE_FORMAT() is the date you want to format. Putting NOW() in there means you will return the current date.
First, you'll need to change the date column to DATETIME, then use that column as the first argument to DATE_FORMAT. Try this:
SELECT *, DATE_FORMAT(mydate ,"%b %d %Y %h:%i %p") as date_added FROM location
Where mydate is the DATETIME column from the table.
See demo
The column need to be in type DATETIME. With date_time_column is a column in location table. Should be like this:
$cms->my_query('SELECT DATE_FORMAT(date_time_column,"%m-%d-%Y %r") FROM location');

Convert datetime to varchar from MSSQL to MYSQL

how to convert this line of MSSQL to MYSQL
SELECT convert(VARCHAR(10), getdate(),106) -- dd mm yyyy
I have no idea how to do it in mysql please help..
I couldn't be sure if this is what you wanted from the question, but you may be able to use this as a possible guideline.
SELECT DATE_FORMAT(NOW(), '%d %m %Y') AS date_variable_name;
You can use DATE_FORMAT for the formatting of date in mysql
select DATE_FORMAT(now(),'%d-%m-%Y') as curr_date
You can use the DATE_FORMAT function of MYSQL
See
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
or in your case
mysql> select DATE_FORMAT(now(),'%d-%m-%Y');

Get week from unix timestamp retrieved from database

I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.
MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51

SQL Query Date Format Two Columns

I have two columns (Date and Time) in database that I would like to get from SQL query with specific date format. It can be two values from sql query or one. Here are the details.
Display Format:
Sunday 03/31/2013 12:13:27 AM
Database Columns:
EndDate (Date yyyy-mm-dd)
EndTime (Time hh:mm:ss)
This is what you need:
DATE_FORMAT(CONCAT(EndDate, EndTime), '%W %m/%d/%Y %r')
The date_format() function gives you everything you need.
Example. Let's say you have columns aDate and aTime in your table, and you need to show the date like 'dd-mm-yyyy' and the time like 'hh:mm'. So:
select
date_format(aDate, '%d-$m-%Y'), date_format(aTime, '%H:%i')
from myTable;
Check the reference manual (Chapter 12 - Functions and operators for MySQL 5.5)
If you need to concatenate the results, then:
select
concat(date_format(aDate, '%d-$m-%Y'), ' ', date_format(aTime, '%H:%i'))
from myTable;
This seems to work for me:
select date_format(concat(enddate,' ',endtime),'%W %m/%d/%Y %r')
from yourtable
SQL Fiddle Demo
Good documentation for date_format: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Categories