Page 1 of 1

Which type of date field should I use?

Posted: Sun Jun 01, 2003 5:50 pm
by Swede78
I'm have a dilemma...

I'd like to store dates in MySQL as YYYY-MM-DD, so that when I transfer dates from my access database, they will convert smoothly.

The problem is that I can't seem to find a way to display the date as MM-DD-YYYY, when I pull them from the MySQL database as YYYY-MM-DD.

I know that I could save each piece of the date into 2 or 3 text parts and switch the order that I print them. But, there's gotta be an easier way.

Is using a timestamp the only way that you can "work" with a date (not just use it as text)?

Let's say I have a date as:

$CurrentDate = date('Y-m-d'); // YYYY-MM-DD

How do I now take $CurrentDate and make it display in the MM-DD-YYYY format?

Posted: Sun Jun 01, 2003 6:03 pm
by riley
One of php's greatest attributes is dates. See http://us4.php.net/manual/en/function.date.php for more information.

Re: Which type of date field should I use?

Posted: Mon Jun 02, 2003 12:54 am
by McGruff
Swede78 wrote:Is using a timestamp the only way that you can "work" with a date (not just use it as text)?

Let's say I have a date as:

$CurrentDate = date('Y-m-d'); // YYYY-MM-DD

How do I now take $CurrentDate and make it display in the MM-DD-YYYY format?
I think you mean (missing argument):

Code: Select all

<?php
$CurrentDate = date('Y-m-d', $timestamp); 
?>
If you have a $timestamp, you can format it any way you like with date().

If you don't have a timestamp, mktime() can create one.

See the mysql manual for date functions such as DAYOF WEEK, MONTH and YEAR.

I often work with timestamps stored in mysql integer columns and mostly ignore all the mysql date stuff - OK as long as you don't need to go further back than 01/01/1970.

Posted: Mon Jun 02, 2003 3:24 am
by volka
you might also use the date_format function in mysql to query the date in a format you like, e.g.
SELECT date_format(myTimestampField, '%c %e %Y') FROM myTable

http://www.mysql.com/doc/en/Date_and_ti ... ml#IDX1305

Posted: Mon Jun 02, 2003 3:26 am
by []InTeR[]
Maybe u need to reed this page of the MySQL manual.