Formatting Dates in PHP
Moderator: General Moderators
Formatting Dates in PHP
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!
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!
http://se.php.net/manual/en/function.date.php has more examples than you can dream of...
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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'
http://us2.php.net/manual/en/ref.datetime.php
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";
?>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.
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.
It depends on where you would like to put the transformation of the fields.
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.
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.2003/09/17 is an example.
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]);-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
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