Convert date string to insert in mysql date format - php

I have a bunch of txt files that contains a lot of data, including a date in format
dd-mm-YYYY
for example this date
15-03-2014
Researching around here i found a way to convert this date to insert in database in the correct date format YYY-mm-dd
the query is
STR_TO_DATE('$array[12]', '%Y-%m-%d')
But i'm having weird results like
2019-07-20
How can i correctly insert them to database any tips of what i'm doing wrong?

The second parameter should be in the format your date is stores, not the format you're looking for which is always YYYY-MM-DD. So:
STR_TO_DATE('$array[12]', '%Y-%m-%d')
should be:
STR_TO_DATE('$array[12]', '%d-%m-%Y')

Related

how to search records beetween two dates. But table date format is varchar

I have a table. In which, a column name is tb_date, which is varchar format. In that column dates are save, But in different-different format (Like: 01/07/201 OR 01-08-2018 or 2017/03/12 etc.).
Now I want a search between given date. But it is not working. I tried it-
SELECT * FROM `user_History` WHERE date_format(str_to_date(`tb_date`, '%d/%m/%Y'), '%d/%m/%Y') BETWEEN '01/06/2018' AND '31/06/2018'
But its giving all record.
I tried it in my sql.
Whats is the problem?
Problem is in your str_to_date(tb_date, '%d/%m/%Y'), you give a format %d/%m/%Y of importing date, but you have different formats in this field.
I think, you should process all your table by PHP and, for example, convert all your dates to UNIX_TIMESTAMP by function strtotime().
It will be more easy than try to create a SQL query for it.

mysql database date field datatype

Hope somebody can advise on the following:
I have created a table into my database which has a coloumn with datatype defined as DATE. The normal MySQL date order is YYYY-MM-DD, however I need to enter it in format DD-MM-YYYY.
I am using php to insert data into my table, and I was wondering at what point should I convert the date format.
To summerize, I am asking the following:
Is it possible to insert into MySQL database where field is formatted as DATE, a date in format DD-MM-YYYY instead in format YYYY-MM-DD?
If not possible, can I convert a value posted from PHP in format DD-MM-YYYY into the format YYYY-MM-DD using the MySQL syntax or I should use PHP to post the value in YYYY-MM-DD format?
Thank you in advance
You would use STR_TO_DATE in your INSERT to convert the date to the ANSI standard date format.
INSERT INTO `table` (`date`) VALUES (STR_TO_DATE('01-05-2013','%d-%m-%Y'));
You can then use DATE_FORMAT to convert the ANSI standard date to the format you want.
A date is always stored in the same manner in the DB.
When inserting you have to give the DB a format that it can parse as date. And when reading from the DB you can format the date in any way you like.
So you have to format your data before inserting into the correct format. Otherwise the DB can't store it as date.
You can go to different mysql website and get this answer. For Example:
http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');

DateTime::createFromFormat

I need to enter dates and time(input as strings) into my database but there's no change (remains as 0000-00-00 for the date and 00:00:00 for the time). I know I'm supposed to use DateTime::createFromFormat() but I'm not sure how to go about it. Please be patient and explain to me how I'm supposed to go about it and what is happening in each step and if I want to return the date from the database on later on, how will I go about it? I'm using $date and $starttime , $endtime.
While inserting, use STR_TO_DATE to convert your String date to Date or DATETIME based on the format as :
INSERT INTO TABLE1(date, startTime, endtime)
VALUES (STR_TO_DATE('$date','%d/%m/%Y'),
STR_TO_DATE('$starttime','%d/%m/%Y a%h:%i:%s'),
STR_TO_DATE('$endtime','%d/%m/%Y a%h:%i:%s'));
Similarly use DATE_FORMAT during retrieval to convert into string of desired format as:
SELECT DATE_FORMAT(date, '%d/%m/%Y') AS formatter_date,
DATE_FORMAT(starttime,'%d/%m/%Y a%h:%i:%s') AS formatted_startTime
DATE_FORMAT(endtime,'%d/%m/%Y a%h:%i:%s') AS formatted_endTime
FROM TABLE1;
Let me know, if you need further explanation.

PHP MySQL date functions

I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.
Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.
Just use:
$date = date('d-m-Y', strtotime($dateFromDB));
That will convert from MySQL DateTime to the format you have specified.
It is possible to order by date fields, e.g.:
SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]
Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.
So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.
If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.
Store the date in default format that is yyyy-mm-dd
when you want to display in front end
use the following query to
select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;
For ordering by date
SELECT * FROM tbl
ORDER BY date DESC

sql - BETWEEN timeframes with a weird timestamp

I have a column that is populated with a date format that is useful for another application but not the mysql format for timestamps. Here is what it looks like:
2010.01.28 12:00 ("time" column) instead of the mysql timestamp: 2010-01-28 12:00:00 ("updatetime" column).
I need to do a date specific search between two time periods and for a normal mysql timestamp I would run this:
SELECT * FROM table WHERE updatetime BETWEEN '$dateStart' and '$dateEnd'
but this doesn't work with the "time" column formatted as it is. I would prefer to keep that column formatted as such as a different application requires that date format, so does anyone know a MYSQL way to search BETWEEN two timeperiods with the 2010.01.28 12:00 format? Running from a PHP script
You can either format the times in the same format in PHP, or in MySQL
WHERE updatetime BETWEEN
date_format('$datestart','%Y.%m.%d %H:%s')
AND date_format('$dateend','%Y.%m.%d %H:%s')
You might want to try something like this
SELECT * FROM table WHERE STR_TO_DATE(updatetime, "%d.%m.%Y %h:%i")
BETWEEN '$dateStart' and '$dateEnd'
to convert the string into a date - that is if the values of $dateStart and $dateEnd are already in the correct format for comparison with a sql date

Categories