Page 1 of 1
Formatting date parts
Posted: Wed Jul 09, 2008 4:34 pm
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?
Re: Formatting date parts
Posted: Wed Jul 09, 2008 4:43 pm
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
Re: Formatting date parts
Posted: Wed Jul 09, 2008 4:45 pm
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
?>
Re: Formatting date parts
Posted: Wed Jul 09, 2008 4:52 pm
by fluidbyte
no, I've got a date I'm pulling in.
Re: Formatting date parts
Posted: Wed Jul 09, 2008 5:00 pm
by Eran
Then my suggestion should work..
Re: Formatting date parts
Posted: Wed Jul 09, 2008 5:16 pm
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
Re: Formatting date parts
Posted: Thu Jul 10, 2008 1:00 am
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.