Formatting date parts

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
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Formatting date parts

Post by fluidbyte »

I know it's a newb question, but how do I format specific dates and times?

I've got a date in YYYY-MM-DD format and I'd like to pretty it up, but I can't find a good answer anywhere.

I've done an explode and have the three peices, but how do I convert them?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Formatting date parts

Post by Eran »

No need to explode() the date. Convert it into a timestamp using strtotime() http://www.php.net/strtotime, and then format it using date() http://www.php.net/manual/en/function.date.php
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Formatting date parts

Post by jaoudestudios »

Do you want the current date?

Use...

Code: Select all

 
<?php
// simple format
echo date('d m Y');
// result
// 09 07 2008
 
// more details
echo date('l dS M Y');
// result
// Wednesday 09th Jul 2008
?>
 
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Re: Formatting date parts

Post by fluidbyte »

no, I've got a date I'm pulling in.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Formatting date parts

Post by Eran »

Then my suggestion should work..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Formatting date parts

Post by Benjamin »

In addition to what pytrin has already recommended, you can also format it from within the database query.

http://dev.mysql.com/doc/refman/5.0/en/ ... ate-format
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Formatting date parts

Post by jaoudestudios »

What format do you have it in? YYYY-MM-DD? You should always store dates and time as a unix time stamp (php, not sql), then you can do whatever you want with it so easily, as php has built in functions.

When storing the current time and date in the database use the php function time();

Then from that you can always use...

Code: Select all

 
<?php
echo date('d m Y', *UNIX TIMESTAMP No.*); // or any format of [b]d m Y[/b]
?>
 
NB: The unix timestamp stores time and date and you can use the line of code above to pull out one or the other or both.
Post Reply