Dynadot โ€” .com Registration $8.99

MySQL date

Spaceship Spaceship
Watch

TwiztedFake

Established Member
Impact
1
On the site I'm working on I store the date in a MySql table every time a member submits a form. The default format for DATE in MySql is YYYY-MM-DD but I want it to display as MM-DD-YYYY when its called to be displayed.

Is there a way to do this in PHP or should I just change it from DATE to something like VARCHAR, which kind of screws up with ordering by date.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
TwiztedFake said:
On the site I'm working on I store the date in a MySql table every time a member submits a form. The default format for DATE in MySql is YYYY-MM-DD but I want it to display as MM-DD-YYYY when its called to be displayed.

Is there a way to do this in PHP or should I just change it from DATE to something like VARCHAR, which kind of screws up with ordering by date.


Store it as YYYY-MM-DD than use explode to rearrange it for PHP processing..

iNod.
 
0
•••
I just discovered that and am reading about it now.
 
0
•••
you can use strtotime( ) , see sample here
 
0
•••
I used a function to convert the date for displaying it on pages.

PHP:
function dateconvert($date) {
		list($year, $month, $day) = split('[-.]', $date); 
		$date = "$day/$month/$year"; 
		return $date;
		}

Then simply used the following where I wanted to display it.

$date = dateconvert($row["date"]);
echo $date;

It works so thought I'd share it.
 
0
•••
The date is stored in mySQL with format YYYY-MM-DD and that's normal behaviour.
You can use the mySQL date_format function in your query to display dates the way you want.

Example:
Code:
SELECT DATE_FORMAT(your_date_field, '%m-%d-%Y') as date_formatted
from your_table
This will display date like this: MM-DD-YYYY
 
0
•••
Why not use timestamp always and format it to whatever in PHP?
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back