Post date in MYSQL format [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Inserting mm/dd/yyyy format dates in MySQL
Users are filling out a form for birthdate in this format MM-DD-YYYY.
I want to insert it into My SQL database in SQL Date format, YYYY-MM-DD.
What is the easiest way to change it? I considered doing a MID function, but was hoping to use something more efficient.
Thanks in advance!
***Yes this was answered before, but I was unable to find it, Thanks for the link!

Try this:
$sql = "INSERT INTO `my_database` (`my_date_field`) VALUES (STR_TO_DATE('".$my_date."', '%Y-%m-%d'));";

Related

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

How to store and publish a date of birth? [duplicate]

This question already has answers here:
Calculate age based on date of birth
(11 answers)
Closed 8 years ago.
I want to store a user's date of birth, publish it and show the age. I know how to store the date of birth, but I do not know how I could echo the age afterwards.
I want it like:
username - 06/02/1992 (22 years)
How do I do this?
Essentially you just need to use the date() function to format it.
This answer will help if you're using MySQL and will also describe the basics of formatting dates.
Convert from MySQL datetime to another format with PHP

Insert date to mysql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP mysql insert date format
I'm trying to insert a date in to my mysql datebase. The problem is that when I for example try to save 2013-01-18 it saves it like this: 000-00-00.
$date = date("Y-m-d");
mysql_query("insert into events values('','$event','$notes','$date')");
What should I do to fix this?
It's better to convert it to Unix timestamp first, and then save the timestamp. It's a lot easier to sort etc.
As for your question: if you really want to save it that way, your column should be a 'varchar'.
Also, mysql_query is deprecated. Don't use that anymore, it won't be supported for long anymore.

PHP Date compare with sql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use DATE FORMAT in my query?
I have a date from form post as 09-23-2012 and in my sql database i save date with datetime format like 2012-09-23 23:11:13
I want to create a query to compare these two date.
These two date should equal.
Try DATE_FORMAT:
WHERE DATE_FORMAT(the_field, '%Y-%m-%d') = CAST(:yourInput AS DATE)
Or something similar

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