I have a column that stores DateTime in a MySQL database. I want to compare the Date part (and not the time) to the current day. I am not sure what method to use as I am very new to PHP.
$today=date("Y-m-d");
$sql="SELECT journey_id FROM tbl_journeys WHERE identity='$driver_id' AND DATE_FORMAT(`date`, '%Y %m %d')='$today'";
I tried this way but it's not working. In the statement, date is a column in the table which I need to trim to only Year-month-day
You're using different characters between the year, month, and day in the two formats.
$today=date("Y-m-d");
puts hyphens between them, while
DATE_FORMAT(`date`, '%Y %m %d')
puts spaces between them.
Change one of them to match the other, e.g.
DATE_FORMAT(`date`, '%Y-%m-%d')
Or you could just do it all in MySQL:
$sql="SELECT journey_id FROM tbl_journeys
WHERE identity='$driver_id' AND DATE(`date`) = TODAY()";
Related
Ok im trying to order results by date and time in an ascending order but so far this isnt working :(
$kalendarquery = mysqli_query($con, "SELECT
people.firstname,
people.lastname,
people.id,
people.avatar,
dates.date,
dates.time,
dates.timezonedate
FROM people
INNER JOIN dates ON
people.id=dates.invited_id
WHERE dates.inviter_id='$user_id' AND status='1'
ORDER BY STR_TO_DATE(CONCAT(dates.date,' ',dates.time), '%d/%m/%y %h:%m') ASC
LIMIT 50");
I mean it doesnt order it by the lowest date to the biggest
Your second parameter to STR_TO_DATE has to match the date format in your char field; otherwise, the conversion will just return NULL for every row which is why its not being ordered.
So since your dates are formatted like 27.03.2015 using . as delimeter, you need to make sure you use . in that parameter rather than /.
Also lower case %y is for 2-digit year, so you need %Y because you're using 4-digits:
ORDER BY STR_TO_DATE(CONCAT(dates.date,' ',dates.time), '%d.%m.%Y %h:%i') ASC
You didn't give an example of your time format, but you can figure out how to do it properly by reading the docs for date_format(). But notice above I changed %m to %i in the time part: you were using %m for both month and minutes which is obviously wrong.
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');
I have a table column named date_created where I store datetime values in VARCHAR e.g. 16-06-2013 10:49:29
I want to get results through mysql query between tow dates. I am using query like this:
... WHERE date_created BETWEEN '06-08-2013 22:30:18' AND '28-08-2013 22:30:22' ...
This query return results but that result also includes older date records that are between 06 and 28 of every month. I also use < and > but these also did not work.
How can I get results that only include records between '06-08-2013 22:30:18' AND '28-08-2013 22:30:22'
You can use MySQL-function STR_TO_DATE to format strings into DATETIME.
SELECT STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s');
# 2013-08-06 22:30:18
I would recommend to reformat all VARCHARs to DATETIMEs with a single UPDATE and fix the code.
you need to use the STR_TO_DATE function to convert your date string into a date that can be used for comparisons.
STR_TO_DATE(date_created, '%d-%m-%Y %H:%i:%s') BETWEEN STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('28-08-2013 22:30:22', '%d-%m-%Y %H:%i:%s')
Using a MySQL SELECT query via PHP, I would like to order my entries by date. The query I'm using is:
$query = "SELECT title, file, oblong_pic, square_pic,
DATE_FORMAT(date, '%W %d %M %Y') AS date
FROM dyhamb ORDER BY date DESC";
I have four entries dated: 24/7/2012, 1/7/2012, 5/6/2012 and 10/4/2012 and would like them to be displayed in that order however they are being displayed as: 24/7/2012, 10/4/2010, 5/6/2012 and 1/7/2012.
I can only think that I've set my query up incorrectly somehow but I don't see where I've gone wrong.
%W in DATE_FORMAT is weekday name, so sort starts with that
If you want to use date alias you should rewrite your sql query to:
$query = "SELECT title, file, oblong_pic, square_pic, date AS sort_date,
DATE_FORMAT(date, '%W %d %M %Y') AS date
FROM dyhamb ORDER BY sort_date DESC";
You have defined a column alias with the same name with an existing column. Thus the ordering occurs based on the DATE_FORMAT function, instead of the date real value.
As a good practice, I suggest to remove the DATE_FORMAT function from the mysql query and leave this part to your php script.
DATE_FORMAT returns string, therefore you are ordering it as string, in which you are using %W, so you are ordering firstly by weekday name. If you want to order by date then just do this:
SELECT title, file, oblong_pic, square_pic, date
FROM dyhamb
ORDER BY date DESC
If you really want to select formated date then add DATE_FORMAT(date, '%W %d %M %Y') as formatted_date to selected columns, note that column alias is not date but formatted_date.
I am developing a PHP application which will handling many company articles.
Now I am creating a page which should order articles BY DATE ie all articles created in a certain day are shown with the appropriate heading and so on for all the articles.
I have used Unix timestamps to save the dates in the MySql db but I cant find code which only sorts dates by days. Can I please get some assistance.
Thanx
You can use mktime() to get the timestamps you would want to use to bin the entries:
http://us.php.net/manual/en/function.mktime.php
$timestamp_for_my_birthday = mktime(0,0,0,10,16,1984);
$timestamp_for_the_next_day = mktime(0,0,0,10,17,1984);
if($time > $timestamp_for_my_birthday && $time < $timestamp_for_the_next_day){
// Time is on my birthday
}
To make this into a MySQL query:
$SQL = "SELECT * FROM dates WHERE date > $timestamp_for_my_birthday AND date < $timestamp_for_the_next_day;";
To order by date I think it would go something like:
SELECT * FROM dates ORDER BY FROM_UNIXTIME(time, '%M %d %Y');
Can you do a
SELECT DATE(FROM_UNIXTIME(my_field_name)) FROM table_x ORDER BY my_field_name
You can order by the timestamp column, but if you want a string representation of which day the record belongs to, you can get any part of the date with the FROM_UNIXTIME(timestamp, 'format') function.
select FROM_UNIXTIME(timestampColumn, '%Y %m %d')
The format values in the second parameter can be adjusted based on the table here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format