Page 1 of 1

Display only Month of a Date

Posted: Mon May 09, 2005 7:09 pm
by wkrauss
I have a database (MySQL) filled with dates in the format YYYY-MM-DD and I need to somehow take a date from the database (lets say 2005-04-15) and show only the month. So the output would be just 04 . I am using PHP/MySQL and already have a recordset set up connecting to the database and all that I just need to somehow "filter" "sort" "format" ,whatever you want to call it, that date. Any and all help will be GREATLY appreciated.

Posted: Mon May 09, 2005 7:51 pm
by Todd_Z

Code: Select all

// Format: YYYY-MM-DD
$date = "2005-05-09";
list ( $year, $month, $day ) = explode ( "-", $date );
echo "Year: $year\nMonth: $month\nDay: $day";

Posted: Mon May 09, 2005 7:55 pm
by Burrito
why not just use the date() function?

ex:

Code: Select all

$date = "2005-05-09";
$month = date("m",strtotime($date));
echo $month;

Posted: Mon May 09, 2005 9:08 pm
by wkrauss
Perfect. Thanks it works perfectly. Now I have another problem with the code below:

I started off with this:

Code: Select all

$days = array(
					$today=>array(NULL,'calendar-day',NULL,NULL.$today.'</span>'),
      			  9=>array('#','calendar-linked',NULL,NULL.$evtDay.'</span>'),
      			  10=>array('#','calendar-linked',NULL,NULL.$evtDay.'</span>'),
      			  11=>array('#','calendar-linked',NULL,NULL.$evtDay.'</span>'),
					);
And with your code I transformed it into this:

Code: Select all

$evtDate = $row_rsAppearances['Date'];
			list ($evtYear, $evtMonth, $evtDay) = explode ("-", $evtDate);
									
					$days = array(
					$today=>array(NULL,'calendar-day',NULL,NULL.$today.'</span>'),
      			  $linkDay=>array('#','calendar-linked',NULL,NULL.$evtDay.'</span>'),
					);
So now, rather than using "9" "10" and "11" it will use the values from the database. Only problem is I cant get it to repeat. It will only write the code of line 6 (in the 2nd php code box) one time rather than once for every date entry in the database.