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);
Related
This question already has answers here:
PHP function to get the date format?
(6 answers)
Closed 11 months ago.
How to get a format of the given date or time in php?
If I give date like this,
2022-03-08 06:45:06
It should return "Y-m-d H:i:s"
Is there any possible way to get the format from date?
i dont think there is such a function.
if you need to handle dates in different formats, reformat them in timestamp and then in the needed format
you can try something like this
$timestamp = strtotime("2022-03-08 06:45:06");
$newFormat = date('Y-m-d', $timestamp)
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
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');
This question already has answers here:
PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa
(5 answers)
Closed 6 years ago.
Im currently struggeling with timestamps. I have Googled for a simple answer for this, but cant find any solution for my problem. I want to convert user input into UNIX time. The user inputs for example "26/03/1982", how do I convert this input into UNIX timestamp?
//User input
$birthday = "26/03/1982"; // d/m/y
$birthday = [CONVERTED TO UNIX]
Short and simple question. I will add more if you'd like. Thanks.
Look into the PHP function strtotime:
strtotime — Parse about any English textual datetime description into a Unix timestamp
I'm not sure how well it handles / in dates, but replace them with - and it'll work.
Example
echo strtotime(str_replace("/","-","26/03/1982"));
outputs 385977600 which is the Unix date for Fri, 26 Mar 1982
You may need to set your date_default_timezone for this to work
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'),