How to Use CURDATE() in FPDF - mysql

I want to put the current date in FPDF Cell.
I used the following query but I don't know how to put it there.
SELECT DATE_FORMAT( CURDATE( ) , '%d/%m/%Y' )

If you just want the current date, you can use PHP's date function:
$currentDate = date("j/n/Y");
If you need to use SQL, you can use this query to achieve the same thing:
SELECT CONCAT(DAYOFMONTH(CURRENT_DATE), '/',
MONTH(CURRENT_DATE), '/',
YEAR(CURRENT_DATE));
Note that these do not include leading series. Date will be in the format of 5/6/2015.

Related

mysql date comparision is not working unable to find the issue

i have a huge data with dates as string.
column name date1
datatype varchar
the stored data is in this format:14-Mar-2016 05:44:38pm
Now I have split only date from this string like this: 14-03-2016
By using this: DATE_FORMAT(STR_TO_DATE(gr.date1, '%d-%M-%Y'),'%d-%m-%Y')
Now I am trying to compare the date with this query:
SELECT * FROM
( SELECT date1,DATE_FORMAT(STR_TO_DATE(date1, '%d-%M-%Y'),'%d-%m-%Y') as dateFormatted
FROM `grabt` ) as mTbl WHERE mTbl.dateFormatted >= '19-01-2016'
AND mTbl.dateFormatted <= '25-01-2016'
but it is not working what could be the possible error.?
The timestamp string 14-Mar-2016 05:44:38pm can be converted to a datetime using the STR_TO_DATE() along with the format string %d-%b-%Y %r. We can then obtain only the date portion by wrapping that with DATE(). Have a look here for a demo to see that this works.
SELECT *
FROM
(
SELECT DATE(STR_TO_DATE(date1, '%d-%b-%Y %r')) AS dateFormatted
FROM grabt
) AS mTbl
WHERE mTbl.dateFormatted BETWEEN '2016-01-19' AND '2016-01-25'
As Gordon already pointed out, you should ideally be using date types not strings for your date calculations. And by the way, use a valid date string when comparing in your WHERE clause. YYYY-MM-DD is a valid format, e.g. 2016-01-19, but 19-01-2016 is not.
Learn to use the right types for columns. Perhaps you are stuck with someone else's really bad decision to store date/times as strings. Perhaps you cannot change that. But, within a query, use the right types!
SELECT mTbl.*,
LEFT(date1, 10) as FormattedDate -- Is this really necessary?
FROM (SELECT date1,
STR_TO_DATE(LEFT(date1, 10), '%d-%M-%Y') as thedate
FROM `grabt`
) mTbl
WHERE mTbl.thedate >= '2016-01-19' AND
mTbl.thedate <= '2016-01-25';
This will do the comparison as dates not as strings.

Date formatiing not working , mysql

I want to compare event filed of my table with current date. I have dates in event column in m/d/Y format i.e "09/24/2015" .
I am using this query for fetching result which get the records of current date but its returning empty result. I have a record for current date. what is wrong in it ?
SELECT *
FROM all_tasks
WHERE DATE_FORMAT( CURDATE( ) , '%d/%m/%Y' ) = DATE_FORMAT( date( event ) , '%d/%m/%Y' )
Assuming that you store the event as some sort of string, you can just do simply something like:
select * from all_tasks where DATE_FORMAT(NOW(),'%m/%d/%Y') = all_tasks.event
Here you go a fiddle sample.

MYSQL Date query syntax not properly formatted

Here is how my column looks like
deeday
"06/07/15"
"02/07/15"
"06/07/15"
"04/07/15"
"06/07/15"
The following query works well
SELECT * FROM Bango ORDER BY STR_TO_DATE( `deday` , '%y/%m/%d' )
What am I missing in the following query to make it work.
SELECT * FROM `Bango` WHERE STR_TO_DATE( `deday` , '%y/%m/%d' ) = DATE_FORMAT(NOW(),'%d/%m/%y')
Thanks
Your query can work as per below-
SELECT * FROM `Bango` WHERE STR_TO_DATE( `deday` , '%d/%m/%y' ) = curdate();
You can use as per below but it will kill the performance, so you can remove " from your field one time-
SELECT * FROM
`Bango`
WHERE STR_TO_DATE( replace(`deday`,'"','') , '%d/%m/%y' ) = curdate();
SQL LIKE Statement on a DateTime Type
If the column type is datetime the LIKE operator doesn't work without converting the value to a varchar string on the fly.

How to split a MySQL field into two and compare string between both fields?

I've a field in my MySQL table financial_year that contains values like below
01-04-2010-31-03-2011
01-04-2011-31-03-2012
01-04-2013-31-03-2014
and I have a date suppose 03-05-2011
and want to get the financial year in which this date lies.
I tried it by using
SELECT financial_year
FROM financial_years
WHERE '03-05-2011' IS BETWEEN SPLIT("-", financial_year)
but it did not work.
Use LEFT() and RIGHT() since the length on your values is fixed and use STR_TO_DATE() to convert your string to date. Here is the example:
SELECT financial_year
FROM financial_years
WHERE STR_TO_DATE('03-05-2011','%d-%m-%Y') >= DATE( LEFT(financial_year,10) )
AND STR_TO_DATE('03-05-2011','%d-%m-%Y') <= DATE( RIGHT(financial_year,10) );
If the data type of financial_year is VARCHAR() you should use STR_TO_DATE() too like on this one
STR_TO_DATE(LEFT(financial_year,10),'%d-%m-%Y')
and
STR_TO_DATE(RIGHT(financial_year,10),'%d-%m-%Y')
The following code did work
SELECT financial_year
FROM financial_years
WHERE STR_TO_DATE('03-01-2012','%d-%m-%Y') >= STR_TO_DATE( LEFT(financial_year,10),'%d-%m-%Y' )
AND STR_TO_DATE('03-01-2012','%d-%m-%Y') <= STR_TO_DATE( RIGHT(financial_year,10),'%d-%m-%Y' );

Mysql - Multiple incorrect date format stored

I am working with data where the developer prior to me has been lazy and not stored the date in a set format. Its in as varchar and not a datetime. Needless to say its left me needing to sort the data.
Its in two formats
1) {dd}/{mm}/{yyyy} {hh}:{mm}
2) {dd}.{mm}.{yyyy} {hh}:{mm}
I would like to ensure that it is always returned in the mysql dateformat. The query below will get the first one.
SELECT
str_to_date( a.rentalstart, '%d/%m/%Y %k:%i' ),
a.*
FROM
jupiter1.table a
ORDER by createtime DESC
How would I combine the two? I would also need it default to a normal mysql datetime if it matches.
{yyyy}-{mm}-{dd} {hh}:{mm}:{ss}
SELECT CASE WHEN a.rentalstart LIKE "%/%/% %:%"
THEN str_to_date( a.rentalstart, '%d/%m/%Y %k:%i' )
WHEN a.rentalstart LIKE "%.%.% %:%"
THEN str_to_date( a.rentalstart, '%d.%m.%Y %k:%i' )
ELSE CAST(a.rentalstart AS DATETIME)
END AS rentalstart_good,
a.*
FROM ...
You can simple do REPLACE. It will turn all records in the format {dd}.{mm}.{yyyy} {hh}:{mm} and convert it to DateTime data type.
SELECT STR_TO_DATE(REPLACE(a.rentalstart, '/', '.'), '%d.%m.%Y %k:%i') newDate,
a.*
FROM jupiter1.table a
ORDER BY newDate DESC
SELECT
str_to_date( replace(replace(a.rentalstart, '.', '-'), '/', '-'), if(a.rentalstart like '%-%-%-%', '%Y-%m-%d %k:%i', '%d-%m-%Y %k:%i' )),
a.*
FROM
jupiter1.table a
ORDER by createtime DESC
Does this solve your problem?
Edited to include your default condition also