SQL - Selecting DATE FORMAT from table and displaying on screen [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Query to convert from datetime to date mysql
(5 answers)
Parse date in MySQL
(2 answers)
Closed 4 years ago.
I have inserte date into my table and it is in the format YYYY-MM-DD, However when displaying this information I would like to display the date the in DD-MM-YYY format. If anyone has an idea how to achieve this please let me know. Please see my sql query below as this query it not working.
// Create query using SQL string
$sql_query = "SELECT title, level, DATE_FORMAT(dateTo, '%W %M %e
%Y')
FROM jobPost ORDER BY jobID DESC";
// Query database using connection
$result = $conn->query($sql_query);

You can use the following:
DATE_FORMAT(dateTo, '%d-%m-%Y')
/* For dateTo value of 2018-11-13, it will output 13-11-2018 */
Details:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
Check the complete list of available format specifiers at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

Related

SQL Select: Multiple values [duplicate]

This question already has answers here:
Datetime equal or greater than today in MySQL
(10 answers)
MySQL Where DateTime is greater than today
(3 answers)
Selecting entries by date - >= NOW(), MySQL
(6 answers)
Select record(s) from mysql table where date is greater than or equal to today
(3 answers)
MySQL query select all were date is equal to today on datetime
(2 answers)
Closed 3 years ago.
Would somebody mind helping me with this?
I'm trying to select all bookings from my table with a start date of today or in the future. The issue i'm having is that I already have a limit (pagination) and sort.
$today = date("Y-m-d");
$sql = "
SELECT *
FROM `bookings`
WHERE startdate >= $today
ORDER BY `startdate` ASC
LIMIT $offset, $no_of_records_per_page";
Edit: The code above displays all bookings regardless of date, rather than just present/future bookings.
Look at the generated SQL statement -- the current date in the SQL query should be something like '2020-01-02' (including the single quotes). Without the quotes, you'd get weird behavior like the database doing the subtraction and comparing the date against 2020-01-02 = 2017!.
WHERE startdate >= $today
Your immediate problem is that you are not surrounding the date variable with single quotes, so you end up with something like where start_date >= 2020-01-13. MySQL sees an arithmetic operation (2020 minus 1 minus 13 = 2006) and happily executes it. Now it needs to compare column startdate (which is of date datatype or the like) to an integer: for this, it implicitely casts startdate to unsigned: this generates a number like 20200113, which is much bigger than 2006. This is not what you intend.
This woud not happen if you were using parameterized queries.
But bottom line, why bother computing the current date from PHP when MySQL has a built-in for that? Just do: WHERE startdate >= CURRENT_DATE and you are all set.

How to get date & month using date in mysql [duplicate]

This question already has answers here:
Compare only day and month with date field in mysql
(5 answers)
Get month name from Date
(40 answers)
Closed 5 years ago.
I want to get date & month from date. example 2017-06-15 so I want date & month i.e. -06-15. Do u have any idea? Really appreciated.
From Below query I am getting month from date. but I want both date & month
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
SELECT DATE_FORMAT(date, '-%m-%d') AS month_name FROM table_name;
see the date_format documentation to see all the possible formats
MySQL MONTH() returns the MONTH for the date within a range of 1 to 12
( January to December). It Returns 0 when MONTH part for the date is
0.
SELECT MONTH('2009-05-18');
MySQL DAY() returns the day of the month for a specified date. The day
returned will be within the range of 1 to 31. If the given date is
‘0000-00-00’, the function will return 0. The DAYOFMONTH() is the
synonym of DAY().
SELECT DAY('2008-05-15');
Link : here
select month('2017-06-15'),DAY('2017-06-15')
you can use this function
DATEPART(datepart,date)
use below query
SELECT DATEPART(yyyy,datecolumn) AS dateYear,
DATEPART(mm,datecolumn) AS dateMonth,
DATEPART(dd,datecolumn) AS dateDay
FROM table_name

how to find the next closest date in with my given date mysql and php [duplicate]

This question already has answers here:
Get closest date from MySQL table
(3 answers)
Closed 7 years ago.
Hi i have some examples dates given below.How to find the next closest date in php or in mysql ?
I have some date like 12-04-2015 so now i need to get the closest date after the given date.So in my case my closest date is 15-04-2015.How can i find using PHP and mysql ?? Any one help me
PHP
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
Mysql
SELECT Birthdate FROM hedging ORDER BY ABS(DATEDIFF(Birthdate , `2015-04-12`)) LIMIT 1
How can i execute the above query in mysql ?
BirthDate
25-03-2015
10-04-2015
10-04-2015
11-04-2015
15-04-2015
30-04-2015
You've not given us much information about what you've tried.
In PHP, store the dates in an array, you can then simply sort it and choose the next value.
In SQL so long as they are stored as dates, then WHERE date > $date ORDER BY date DESC LIMIT 1 will give you the next date.

SQL,PHP: Converting Date Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I'm having a bit of a problem where both the SQL function and the php function does not convert dates properly when they're in the format of "DD-MM-YYYY". I am using a mysql database and I want to convert the date to "YYYY-MM-DD" before it is entered into the database.
The SQL function:
INSERT INTO lease
VALUES(3, 4, STR_TO_DATE('22-02-2015', '%Y-%m-%d'), STR_TO_DATE('27-02-2015', '%Y-%m-%d'))
Returns as "2022-02-20" and "2027-02-20"
Also, the php function puts the month and day in the wrong place so I have to do "Y-d-m" instead, like so:
$startdate = date("Y-d-m", strtotime('27-02-2015'));
$enddate = date("Y-d-m", strtotime('23-02-2015'));
Although that stores it correctly. Any date where the day is > 12 will reset the date to"1970-01-01" because it thinks the day is the month.
Can anyone help with this?
The format argument for STR_TO_DATE() is the format used for the string argument
So use
STR_TO_DATE('22-02-2015', '%d-%m-%Y'),

create time stamp and compare time by mysql field [duplicate]

This question already has answers here:
MySQL convert date string to Unix timestamp
(4 answers)
Closed 8 years ago.
there is a table with these fields
month
year
day
and these files are contain numbers like
year = 2001 and month = 10 and day = 25
and i have time stamp in php
so i need to compare these in mysql
something like:
select * from times where mktime(year,month,day)<1235478554
is there any function in mysql for this?
i need to make time stamp in mysql with the mysql fields...
UPDATE 1 :
i used like this and not worked
SELECT * from work_result where UNIX_TIMESTAMP('year-month-day 00:00:00')<1
UPDATE2:
There's UNIX_TIMESTAMP which will get you the Unix timestamp of a given date string. Which means you need to build a date string inline in your MySQL query. Doing so is considered bad style, you should propably be storing a Unix timestamp within your database.

Categories