How to convert date format in mysql dynamic query [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I want to insert mm/dd/yyyy to mysql how to convert into default format and I need dynamic query for that
$sql="INSERT INTO user_db (EMAIL,PWD,SQUE,SANS,FNM,LNM,DOB,GENDER,ROLE) values
('".$email."','".$pass."','".$sq."','".$ans."','".$fname."','".$lname."','".???."','".$gen."','".$role."')";
user enter mm/dd/yyyy format convert into default format

There is no way to change what date literals MySQL accepts. They are listed in the documentation here, and are fixed. Instead, you have to change the format of the date you input. Answers to this question will tell you how.

Date format in database is always yyyy-mm-dd format
But While fetching, you can fetch in dd-mm-yyyy format as specified below.
SELECT DATE_FORMAT(datecolumn,'%d-%m-%Y') AS dateColumn FROM table

Related

How to change date from database to dd/mm/yyyy - php in codeigniter [duplicate]

This question already has answers here:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
(9 answers)
Codeigniter Change Fetched Date format in View
(2 answers)
Closed 4 years ago.
MySQL stores the date in my database (by default) as YYYY-MM-DD The field type for my date is DATE (I do not need any time storage). Is there a simple way to change it by default to DD/MM/YYYY?
The internal storage format of dates in MySQL is usually out of scope for developers and most users of the database. Basic good practice is to always store date information using a proper date column, such as datetime. With regard to how to view your date information in different ways, you may use the DATE_FORMAT function. For example:
SELECT DATE_FORMAT('2014-02-01', '%d/%m/%Y') AS new_date
FROM dual;
01/02/2014
Demo
Simply use date() and strtotime()
date('d/m/Y', strtotime('now'));
OR
date('d-m-Y', strtotime('now'));
OUTPUT
06/06/2018
06-06-2018

PHP script can not format the date [duplicate]

This question already has answers here:
convert php date to mysql format
(9 answers)
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a datepicker that shows the date in the following format: date('d/m/Y') the date is represented as: 29/06/2017.
The date column in my database accepts the format: Y-m-d. Witch is represented as: 2017-06-29.
I am trying to convert the date with the following script:
$single_cal4 = date("Y-m-d",strtotime($single_cal4));
When I execute the PHP script that sends the date to the database the date will not insert. I get the date 1970-01-01 in my database.
Does someone know why this is happening and what I am doing wrong?
Maybe reason is $single_cal4. You make sure that $single_cal4 is valid date string. In other way you can try this:
$myDateTime = DateTime::createFromFormat('d/m/Y', $single_cal4);
$newDateString = $myDateTime->format('Y-m-d');

Change type of date in html table - php mysql [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need some help with converting the date to dd/mm/YYYY.
I have this code to display my date in a html table from a mysql database
echo "<td>".$zeile['datumFahrt']."</td>";
Can I convert it in this line so the format will be dd/mm/YYYY instead of YYYY/mm/dd?
Sure you can, first you need to convert it to timestamp and then format it:
date("d/m/Y", strtotime($zeile['datumFahrt']))

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'),

PHP date time and mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
PHP - Convert to date format dd/mm/yyyy
mysql saves my date and times like this...
2011-08-31 08:48:40
And when I echo them from the database this is how they appear, how can I get them to a universal format like m/d/y...
Or does it not matter if the mysql row is not a DATE_TIME and just VARCHAR and insert it into the database how I want it??
Thanks...
Do it in PHP:
date("m/d/Y", strtotime('2011-08-31 08:48:40'));
Or do it in MySQL:
SELECT DATE_FORMAT('2011-08-31 08:48:40','%m/%d/%Y')
Of course you won't use hardcoded values in your code. You'll use the column name for MySQL or the retrieved column value, that's probably an element in an array, in PHP.
This post is previously answered:
Convert to date format dd/mm/yyyy
Regards

Categories