Display only Month of a Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wkrauss
Forum Newbie
Posts: 3
Joined: Mon May 09, 2005 7:04 pm

Display only Month of a Date

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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";
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

why not just use the date() function?

ex:

Code: Select all

$date = "2005-05-09";
$month = date("m",strtotime($date));
echo $month;
wkrauss
Forum Newbie
Posts: 3
Joined: Mon May 09, 2005 7:04 pm

Post 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.
Post Reply