Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Convert numbers to month and day?
Old 06-24-2005, 09:45 AM Convert numbers to month and day?
Average Talker

Posts: 15
Trades: 0
I have two fields in a table that I store numbers for the month and the day. How can I take those numbers and convert them to where the months are January, February, March (instead of 1, 2, 3) and the days are 12th, 23rd, 30th (instead of 12, 23, 30)?

I don't know if this matters, but the single digit numbers are stored without zeros in front of them.

I know that this is possible with the date function, but I was wondering if there was any way of taking a number and doing the same thing that the date function does.

Thanks,
J2
jacannon2 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-24-2005, 10:53 AM
desertland's Avatar
Skilled Talker

Posts: 86
Trades: 0
This is the reason you should be using date fields to store date/time values. ...but regardless, you can fake it

Try this:

select date_format('2005-04-01', '%M');

That will return 'April'

So will:
select date_format(concat('2005-', '04', '-01'), '%M');

All you need to do is change the '04' to the column that stores our month number.
__________________
I store my recipes online (the way nature intended) at
Please login or register to view this content. Registration is FREE
desertland is offline
Reply With Quote
View Public Profile
 
Old 06-24-2005, 07:09 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Dude - do yourself a favour and invest the time in rewriting your scripts to use date() and combine your database table colums to a single column using the date data type. Like desertland says - that's what they're there for!
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 06-25-2005, 09:51 AM
Average Talker

Posts: 15
Trades: 0
Yeah, I have a date field in my table, but I wasn't sure how to use each part of the date separately in my script. Maybe ya'll can help me...

Code:
<?php
$query2 = mysql_query("SELECT Name, month, day, year FROM birthdays WHERE month > $month ORDER BY month, day")

or die("Query2 failed:<br>" . mysql_error());

while($row = mysql_fetch_assoc($query2)) {
	extract($row);
	$thisyear = date("Y");	$age = ($thisyear - $year);
	$date = ("$month-$day-$thisyear");?>
	<item>	
	<title><?print ("$Name turns $age");?></title>
	<description><?print ("on $date");?></description>
	</item>
<?php}?>
This is part of my script. I have a day, month, year, and date field in my table. The date has the right type, but like I said, I don't know how to extract each separate number so I can use it like I need to.

Also, I didn't code all of this myself. I'm a newbie when it comes to working with databases. I just learned PHP as well and I pretty much tweaked the script when I got it so it would work the way I wanted it to.

If this isn't enough info to help me, just let me know. I had already wanted to do what ya'll are saying to do, but like I said, I'm new at this and didn't know how.

Thanks. -J2
jacannon2 is offline
Reply With Quote
View Public Profile
 
Old 06-25-2005, 07:40 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
here's an example of how to separate the date elements from a date stored in the format 1970:01:01;

PHP Code:
<?php
// retrieve the date from the database column and place in var
// this example will just use a fresh date
// ensure you know how the dates are formatted in mySql i.e. Y:m:d

$thisDate date('Y:m:d');

// assign the values to vars using list() and splitting the date string by the spearating char

list( $thisYear$thisMonth$thisDay ) = split(':'$thisDate);

?> 
<html>
<head>
<title>Extracting Date Elements</title>
</head>
<body>
<h1>The date retrieved is: <?php echo $thisDate ?></h1>
<p>It's Day element is: <?php echo $thisDay ?></p>
<p>It's Month element is: <?php echo $thisMonth ?></p>
<p>It's Year element is: <?php echo $thisYear ?></p>
</body>
</html>
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Reply     « Reply to Convert numbers to month and day?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14849 seconds with 12 queries