SQL,PHP: Converting Date Format [duplicate] - php

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

Related

Change Date format from 2018-09-27T18:30:00.000Z to 2018-09-27 13:17:14 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
When I select a date using angular md-datepicker I receive date in 2018-09-27T18:30:00.000Z this format but in my database i have dates in 2018-09-27 13:17:14 this format, how can I convert dates in PHP to search in the database please help me.
First create DateTime object then change to format whatever you want.
$dateTime = new DateTime('2018-09-27T18:30:00.000Z');
$databaseFormat = $dateTime->format('Y-m-d H:i:s');

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

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

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

php formatting dates for mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert php date to mysql format
I have a date field which lets users type in their birthday in m/d/Y format (for example 06/01/1982) but just in case I also made my validation accept month and day values without any leading 0 (6/1/1982).
How can I convert these dates to Y-m-d for use in MySQL?
Other valid dates are 6/01/1982 and 06/1/1982.
You could use date() and strtotime() together like such:
$date = date('Y-m-d', strtotime('06/01/1982'));
That will give you
1982-06-01
I would validate all input using strtotime (which will handle all major date formats).
On the insert, cast it to the proper format using the inverse: strftime:
$userValidatedDate = strtotime($_GET['date']);
//...
$sqlDate = strftime('%Y-%m-%d',$userValidatedDate);

Categories