Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
I have a list of dates in a database where the months area displayed as 2 digit numbers. I want to extract them and print them as months (january etc) below is my attempt to do this, which seems to correct syntax and does something, but not quite what I expected (the months all come back as september, october or november when there should e other months.... Any idas appreciated....


<?php

$SQL = " SELECT * FROM coursedescriptions WHERE id > 0 ORDER BY coursetitle DESC ";
$retid = mysql_db_query($db, $SQL, $cid);
if (!$retid) { echo( mysql_error()); }



while ($row = mysql_fetch_array($retid)) {

$startday = $row["startday"];
$startmonth = $row["startmonth"];
$startyear = $row["startyear"];

if ($startmonth == 01) {$startmonth = "January";}
if ($startmonth == 02) {$startmonth = "February";}
if ($startmonth == 03) {$startmonth = "March";}
if ($startmonth == 04) {$startmonth = "April";}
if ($startmonth == 05) {$startmonth = "May";}
if ($startmonth == 06) {$startmonth = "June";}
if ($startmonth == 07) {$startmonth = "July";}
if ($startmonth == 08) {$startmonth = "August";}
if ($startmonth == 09) {$startmonth = "September";}
if ($startmonth == 10) {$startmonth = "October";}
if ($startmonth == 11) {$startmonth = "November";}
if ($startmonth == 12) {$startmonth = "December";}




echo ("$startday / $startmonth / $startyear");



}
?>
 
Instead of:

if ($startmonth == 01) {$startmonth = "January";}
if ($startmonth == 02) {$startmonth = "February";}
if ($startmonth == 03) {$startmonth = "March";}
etc

What happens if you do:

if ($startmonth == '01') {$startmonth = "January";}
if ($startmonth == '02') {$startmonth = "February";}
if ($startmonth == '03') {$startmonth = "March";}
etc.

?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi

Because the numbers beginning with zero ( 0 ) are treated as octals. And in octal numbers there are no 8 and 9 digits, so 08 and 09 will be evaluated to 0.

By the way, I would prefer to use an array in that situation.
Code:
<?php
[gray]// ...[/gray]

[red]$monthname=array("January","February","March","April","May","June","July","August","September","October","November","December");[/red]

while ($row = mysql_fetch_array($retid)) {

  $startmonth = $row["startmonth"];

  echo ("$startday / [red]$monthname[[/red]$startmonth[red]-1][/red] / $startyear");

}
?>

Feherke.
 
Thanks, loosing the 0's has fixed the problem. I ut them in because the numbers in the database have leading o's, but if I just take the 0's out from the scrit (even though there still in the database) it works. Doesn't seem to make any difference whether the numbers are in quotes or not.
 
Why not simply use existing functions for this rather than reinvent them?

If your date is truly in three separate fields rather than a single field you could use something like:
Code:
$dat = strtotime("{$row['startyear']}-{$row['startmonth']}-{$row['startday']}");
strtotime() returns a timestamp value. Afterwards strftime() could be used to return whatever format you need to display that information:
Code:
echo strftime("%d/%B/%y",$dat);

I realize you have a working solution already, I just prefer to not reinvent things as that tends to make the code longer and that little bit more difficult to manage when someone comes back and asks for changes in 6 months,

-T

 
thanks, I'll remember that next time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top