PHP date time and mysql [duplicate] - php

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

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

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

Convert date stored as VARCHAR in MYSQL to MYSQL Compatible Date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a database that I receive every month that I simply import through CSV into MYSQL. I don't worry about preserving any old database entries, so I just overwrite the old database with the new entries monthly.
I want to be able to sort through the database by month, however I currently need to import the date field as VARCHAR, or risk having to manually convert tons of entries to fit the formatting of the MYSQL date field.
I would like to take the entries stored in the 'Expiration Date' field and be able to run a query that will convert them to a MYSQL date format and then write them to a new column named 'Expiration_Converted' so that I can then run date functions to show me all dates from a month, year, etc.
I know about the STR_TO_DATE function in MYSQL, however I'm not quite sure how to pull this off. I'm writing everything in PHP.
Any advice would be greatly appreciated.
Thanks!
Try this one.
I'm still using this script in my web application when I get the value from a text field and need to converto to MySQL DATE Format:
$format = '/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/';
if ($_date != null && preg_match($format, $_date, $parts)) {
$date = date_create($parts[3].'-'.$parts[2].'-'.$parts[1]);
$final_formated = date_format($date, "Y-m-d");
return $final_formated;
}
PS: $_date variable receives a date string.
After running this script, I can store date in Database.
Hope it helps you.

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

Categories