Page 1 of 1
Formatting Dates in PHP
Posted: Mon Sep 22, 2003 1:14 pm
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!
Posted: Mon Sep 22, 2003 1:21 pm
by JAM
Posted: Tue Sep 23, 2003 3:48 am
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
Posted: Tue Sep 23, 2003 12:21 pm
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
Posted: Tue Sep 23, 2003 12:30 pm
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.
Posted: Tue Sep 23, 2003 2:25 pm
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.
Posted: Tue Sep 23, 2003 2:36 pm
by tdelobe
thanks!
Posted: Tue Sep 23, 2003 6:24 pm
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);
Posted: Wed Sep 24, 2003 5:34 am
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