开发者

PHP help with image source changing depending on date?

What I want to do is show three images. The images that show up depend on the month. There should be an image for the previous month, the current month, and the next month.

This is what I have..

  //Dates
  $prevDate = date("M Y", strtotime("-1 months"));
  $currDate = date("M Y");
  $nextDate = date("M Y", strtotime("+1 months"));

  $prevMonth = $prevDate.date("M");
$currMonth = $currDate.date("M");
$nextMonth = $nectDate.date("M");

//Years
$prevYear = $prevDate.date("Y");
$currYear = $currDate.date("Y");
$nextYear = $nextDate.date("Y");


  echo '<img src="./images/' + $prevMonth + '_' + $prevYear + '.jpg"/>';

What I end up getting is "0" on the page. I ha开发者_C百科vent worked with php for about 2 years so I am really rusty! any help?

What I would want it to do is have it connect to images named "december_2011.jpg"


This piece of code :

//Months
  $prevMonth = date("M");
  $currMonth = date("M");
  $nextMonth = date("M");

will give the same value for the 3 variables : the current month : "Dec"

This mean :

//Years
  $prevYear = $prevMonth.date("Y");
  $currYear = $currMonth.date("Y");
  $nextYear = $nextMonth.date("Y");

will give you the same things too : "Dec2010" (. is for concatenation in PHP)

Finally :

echo '<img src="./images/' + $prevMonth + '_' + $prevYear + '.jpg"/>';

should be

echo '<img src="./images/'.$prevMonth.'_'.$prevYear.'.jpg"/>';

"." for concatenation and not "+" like in javascript

You could resolve this this way :

//Dates
$prevDate = date("M_Y", strtotime("-1 months"));
$currDate = date("M_Y");
$nextDate = date("M_Y", strtotime("+1 months"));

echo '<img src="./images/'.$currDate.'.jpg"/>';
echo '<img src="./images/'.$prevDate.'.jpg"/>';
echo '<img src="./images/'.$nextDate.'.jpg"/>';

To help you what it renders, here is the codepad example.


Unlike many other languages . is the concatenation operator, not the syntax for 'member-of' (that would be ->). Besides, the interface for date is procedural (no objects) anyway.

Consider this:

function filename_of_time($time) { 
  return '<img src="./images/'.date('M_Y',$time).'.jpg"/>');
}

echo filename_of_time(strtotime("-1 month"));
echo filename_of_time(time());
echo filename_of_time(strtotime("+1 month"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜