Which type of date field should I use?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Which type of date field should I use?

Post 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?
User avatar
riley
Forum Commoner
Posts: 45
Joined: Thu May 02, 2002 6:31 pm

Post by riley »

One of php's greatest attributes is dates. See http://us4.php.net/manual/en/function.date.php for more information.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Which type of date field should I use?

Post 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.
Last edited by McGruff on Thu Aug 11, 2005 7:26 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Maybe u need to reed this page of the MySQL manual.
Post Reply