PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 31st, 1999, 03:23 AM
DBrown
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
I don't want to sound stupid about my lack knowledge of programing and PHP, I'm really new. So I have looked through all the PHP topics on this forum and searched places like devshed, PHPBuilder, the Wizards and Leon Atkinson's book it sitting in front of me. It's 3:3something a.m. and my brain has started to show signs of the Y2K bug.

I want to format a date queried from my DB. I've managed to get the typical 2000-3-21 date but I need to rearange it to look like Mar. 21 2000. It's probably very simple but I can't seem to get a clear and simple answer with an example. I'm also learning Cold Fusion and it's a piece-o-cake to do this in that, but I want to learn PHP.

CODE:
The normal SELECT * from seminars_db stuff has been made and my WHILE loop is running. This is the code I have had the most success with.

$ID = $row[0];
$bdate = $row[1];
$edate = $row[2];
$presentation = $row[3];
$place = $row[4];
$city = $row[5];
$state = $row[6];
$sponsor = $row[7];
$connect = $row[8];

<snipped html>

print date("M. j, Y",mktime($bdate));

I know, this is wrong. At least I got the format right, the date was Dec. 31, 1969 on all records (what's up with that).

I have learned that $bdate is retrieved as a string and that can't be used with the date function.? I have seen the UNIX_TIMESTAMP and the mktime commands mentioned here, and second queries for just the "bdate" field (not sure if that's effiecient) but Leon's book hasn't helped me and the forum has not answered it thorough enough that I could understand. There has been sucess with the basic date function . . .

$Today = date("F d,Y");

. . . and calling it later in the page, and I know I can use this against the "bdate" field. However, I don't need how many days until the event, I need the event M. j, Y pulled from the DB.

Hey, it's past 4:00 a.m. Shouldn't I be wishing someone on the rock Happy New Year?

TIA
DBrown

Reply With Quote
  #2  
Old December 31st, 1999, 06:15 AM
cbolt
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
This is a bit of a kludge but:

$dbmonth = (int)substr($curdate, strpos($curdate, "-") + 1, strrpos($curdate, "-") - strpos($curdate, "-") - 1);
$dbday = (int)substr($curdate, strrpos($curdate, "-") + 1);
$dbyear = (int)substr($curdate, 0, 4);

print date("M. j, Y", mktime(0, 0, 0, $dbmonth, $dbday, $dbyear));

Reply With Quote
  #3  
Old December 31st, 1999, 09:01 AM
rod k
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
The reason date() doesn't work for you is that date() requires a UNIX timestamp to work and you are using a MySQL timestamp. You can use the MySQL DATE_FORMAT function in your query to format the date for you.

$result=mysql_query("select DATE_FORMAT(bdate,%a. %e, %Y) from seminars_db");

Where bdate is the column name of the timestamp you wish to convert. The %a returns the abbreviated month name, the %e is the day of the month 1...31 and %Y returns the 4 digit year. The . and , are returned as is so from 2000-3-21 you'd get:

Mar. 31, 2000

You can see more formatting options in the mysql manual under date/time functions (section 7.3.10 of the 3.22 manual or section 7.4.11 of the 3.23 manual).

Of course, this means you won't be able to use * in your select but have to ask for each column.

Reply With Quote
  #4  
Old December 31st, 1999, 11:07 AM
rod k
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Ooops. I don't know why I assumed that you were using MySQL.... well, if you are, there you go. If not, ignore my previous post.

Reply With Quote
  #5  
Old December 31st, 1999, 11:43 AM
DBrown
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
It Works!

Sorry to get so wordy in my question, I was getting a little slap-happy.

Am I right to assume that CBOLT's answer uses PHP to reformat the date by a generic manipulation of a string and ROD K uses MySQL to reformat the date from the query? I really want to learn this, so reply if you have the time.

BTW, I am using mySQL and I did read over the 3.23 manual section 7.4.11 but I didn't get how this was used. It didn't seem effecient to name all the fields in the query b/c Cold Fusion allows me to manipulate just about any feild with SELECT *. I'm not saying CF is better, it has just been my progression of learning languages.

Reply With Quote
  #6  
Old December 31st, 1999, 05:49 PM
cbolt
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Yes, my answer was using string manipulation, and rod k's answer (or the right answer :-) used mysql... never knew about the DATE_FORMAT function until now.

Reply With Quote
  #7  
Old January 1st, 2000, 01:37 PM
rod k
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Well, I wouldn't say mine was any more right than cbolt's. Just a matter of opinion. Using PHP to format can be lengthy (and takes longer, usually) but can make your query easier to build. To each his own.

The added benefit to doing it in PHP as cbolt described is you can always make it a function and include() the function in the necessary scripts. Of course, the optimal solution would be a compiled plug in of a function that could be added to PHP that would allow date formatting of just about any MySQL timestamp. Unfortunately, I'm not enough of a C programmer to take that on.

Reply With Quote
  #8  
Old January 1st, 2000, 02:16 PM
moderator
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
test . . please ignore.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Formatting a queried date datatype . . . how?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap