Formatting Dates in PHP

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
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Formatting Dates in PHP

Post by tdelobe »

Can someone help me with using PHP to format a date that is stored in the DB like this, 2003/09/17, but I would like it to show up like this on the page...

Wednesday, September 17, 2003

Ideally I would like to be able to tweak the code whenever necessary to get the desire format for the date on the page. Sample code would be great, thanks!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

http://se.php.net/manual/en/function.date.php has more examples than you can dream of...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What type of database is the information stored in? Is it stored in a field type that is specifically intended for dates? If so, check the manual for your database and you should be able to format the date in your SELECT statement.

Mac
phpfre@k*
Forum Newbie
Posts: 1
Joined: Tue Sep 23, 2003 12:21 pm

Post by phpfre@k* »

hi

i hope this example help you. you need to store something similar 2 the timestamp instead of
a 'date' b/c it is un-necassary to format a 'date' to a different 'date'

Code: Select all

<?php
#store time with time() and not date(...) - eg:
$timestamp = time();

#date format:
$date_format = 'F, D j, Y H:i:s A T';

#make date:
$date = date($date_format, $timestamp);

#output:
echo "local time: $date";
?>
http://us2.php.net/manual/en/ref.datetime.php
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

Not sure if this really works for what I am trying to do. Dates are stored in the DB already. I do not want to change the way they are stored. 2003/09/17 is an example.

Using that I wanted to do something similar to formating the date in the Select Statement, but instead do it in the code with PHP.

This brings up a good Philosophical question though. What is the general rule when formatting, should you do it in the SELECT statement, or with the progamming language?

Either way, I am trying to use PHP to do it here. I think this latest example should me a formatted date based on a timestamp which is not what I am using.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

It depends on where you would like to put the transformation of the fields.
2003/09/17 is an example.
I might be of, but I read that as a varchar (or similiar) field, ie. the db itself doesn't at that time understand that its a date. If so, formating it in php would be a preferred choise.

Code: Select all

// not tested myself, but might give more ideas
 // use the / and explode the string
 $exploded = explode("/",$yourdate);
 // set $newdate to a unix timestamp, that you later can date() on
 $newdate = mktime(0,0,0,$exploded[1],$exploded[2],$exploded[0]);
If you on the other hand have the dates in the db as timestamp/date/datetime/time/other-field, I'd personally use try to format the date in the SELECT rather than letting PHP do the job.
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

thanks!
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i suggest using a varchar field to store the date as a number, using funcionts like time() :), and then to read it just use date("format",$timestored);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you were to store the date as a timestamp (as has been suggested I understand that you don't wish to do that but this may help someone else) don't store it in a VARCHAR field, but in an INT field, cause it's always going to be an integer and it's always a good idea to use the most appropriate field for the job.

Personally, I always store my dates in the fields the DB sets aside for dates and times and then format the output in the SELECT, into UNIX timestamps if I need to work with the date further in PHP and/or into the way I want the date displayed. It saves on a bunch of code in PHP especially since the database (in my case MySQL) tends to do the work more efficiently.

Mac
Post Reply