tags:

views:

131

answers:

6

I am working on a website using a PHP script to display information formatted a certain way from a MySQL database. I want the date values from the database (which are formatted as YYYY-MM-DD) to be displayed as Month YYYY. The perhaps-not-efficient-enough code I'm using to display the months as words is below:

foreach($monthstart as $monthnext)
    {
    preg_match("/[[0-9][0-9]]*/", $monthnext, $startmonthfull);
    }
echo "<b>Duration: </b>";
foreach($startmonthfull as $which)
{
   if($which == 01)
{
echo "January ";
}
elseif($which == 02)
{
echo "February ";
}
elseif($which == 03)
{
echo "March ";
}
elseif($which == 04)
{
echo "April ";
}
elseif($which == 05)
{
  echo "May ";
}
elseif($which == 06)
{
  echo "June ";
}
elseif($which == 07)
{
  echo "July ";
}
elseif($which == 08)
{
  echo "August ";
}
elseif($which == 09)
{
  echo  "September ";
}
elseif($which == 10)
{
  echo "October ";
}
elseif($which == 11)
{
  echo "November ";
}
elseif($which == 12)
{
  echo "December ";
}
}
foreach($year as $printyear)
{
  echo $printyear;
}

This code displays the month and year for the first part. This displays perfectly on the webpage, unless the month is August or September. If the month is August or September, nothing gets printed for that part, so instead of "August 2005") you'd get "2005".

The August and September elseif statements are formatted (from what I can tell) exactly the same as the others so I have no idea why these values are not printing. If I simply ask it to print the $which value, it prints as either 08 or 09 and the values look proper in the database. I'm really hoping there is something simple I am overlooking that someone will quickly point out because I have been banging my head against the wall for quite some time with this issue.

(I'm not sure if it makes any difference, but I am using the Jumi plugin in Joomla which allows users to add custom code in a Joomla page to develop this section of the website.)

EDIT: I just removed the 0s from 08 and 09 and now they all display properly. So I guess now the question is why did this fix the problem when in the variable that is being checked the values are 08 and 09?

Also, sorry for solving it so shortly after posting a question.

+1  A: 

Numbers in php starting with a 0 are considerd octal. This could be the reason why. Put "" around them.

But please read the link in the comment to your question. This is considered to be an anti-pattern.

Ikke
Thanks for the information! It looks like my code will end up being about a fifth the size. Question answered in 2 minutes, too!
Greg Zwaagstra
+1  A: 

yes you have to erase the leading 0 on your condition or php would interpret it as octal, you code in better way

foreach($monthstart as $monthnext){
    preg_match("/[[0-9][0-9]]*/", $monthnext, $startmonthfull);
}
echo "<b>Duration: </b>";
$monthArray = array(
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December');

foreach($startmonthfull as $which){
 echo($monthArray[$which] . " ");
}
foreach($year as $printyear)
{
  echo $printyear;
}
RageZ
+3  A: 

You're using Joomla! 1.5, right? Why not letting Joomla! do that work:

if (!defined('MY_DATE_FORMAT'))
   define('MY_DATE_FORMAT', '%B %Y');
jimport('joomla.utilities.date');

$mysql_date = '2009-08-01';

$jdate = new JDate($mysql_date);
echo $jdate->toFormat(JText::_(MY_DATE_FORMAT));

As a side benefit, he will even translate the month into the right/used language if you ever choose to translate your website.

giraff
As I said above, thanks for the info and I'll be sure to try this once everything works as well. I had no idea that I could do that whole jimport sort of thing. That appears to open up a lot of possibilities
Greg Zwaagstra
+2  A: 

Let MySQL format the date with date_format and return it to PHP in the way you want. This way, you don't need to do any manipulation with PHP.

Here is an example query you would make to MySQL:

select date_format(date_column, '%M %Y') as formatted_date from table;

Where:

  • date_column is the original table column that stores the date you want to format,
  • %M means you want the month name spelled out in full ('January', 'February', 'March', etc), and
  • %Y means you want the 4-digit year.

A field in date_column with value of '2009-09-17' will return as 'September 2009'

For example, the following output displays the original date_column values along with the date_format() values:

+-------------+----------------+
| date_column | formatted_date |
+-------------+----------------+
| 2009-09-11  | September 2009 |
| 2009-09-02  | September 2009 |
| 2009-09-01  | September 2009 |
| 2009-08-31  | August 2009    |
| 2009-08-04  | August 2009    |
| 2009-08-01  | August 2009    |
| 2009-07-11  | July 2009      |
| 2009-07-01  | July 2009      |
| 2009-06-30  | June 2009      |
| 2009-06-20  | June 2009      |
| 2009-06-09  | June 2009      |
| 2009-05-26  | May 2009       |
| 2009-05-09  | May 2009       |
| 2009-05-08  | May 2009       |
| 2009-04-22  | April 2009     |
| 2009-04-20  | April 2009     |
| 2009-04-19  | April 2009     |
| 2009-03-05  | March 2009     |
| 2009-03-04  | March 2009     |
| 2009-03-03  | March 2009     |
+-------------+----------------+
shufler
just noticed the joomla tag, this answer may not be appropriate. giraff's answer is probably better suited.
shufler
+1  A: 

This way ensures it works for all locales and it uses things that were written for you a long, long time ago.

mysql:

select UNIX_TIMESTAMP(time) as unixtime from table;

php:

$formatted_time = date('F Y', $row['unixtime']);

It also allows you to reuse the $row['unixtime'] variable so you can format it different ways or reuse it without calling mysql again in the same script. You could even provide a different string from a constant or from some user configurable options to the date function.

Shawn Leslie
+1  A: 

PHP has several functions (and as of PHP 5.3, the DateTime type) to deal with times and dates.

For instance, in PHP 5.x:

$mytime = strtotime($monthstart);
// $output_date = date("F Y", $mytime);
$output_date = strftime("%B %Y", $mytime);

(strftime is included because it's generally portable across programming languages)

R. Bemrose
DateTime actually exists since PHP 5.2, but surely enough, was enhanced in 5.3.
giraff
@giraff: Really? Hmm, I didn't know that. I thought I saw in the docs for DateTime that it was new in 5.3.
R. Bemrose