开发者

the name of the previous month

开发者 https://www.devze.com 2023-03-18 13:56 出处:网络
How would you print the name of the previous month please? I\'m writing Drupal 7 blocks representing \"the miss/mister of the last month\" at my site and wonder how to do it best:

How would you print the name of the previous month please?

I'm writing Drupal 7 blocks representing "the miss/mister of the last month" at my site and wonder how to do it best:

the name of the previous month

The additional difficulty is that my website is in Russian and the names are declinated.

Currently I've hardcoded month names in an array. And I don't know how to find the corrent index in that array:

function pref_block_view($block_name = '') {
  $MONTHS = array(
    'января',
  开发者_StackOverflow中文版  'февраля',
    'марта',
    'апреля',
    'мая',
    'июня',
    'июля',
    'августа',
    'сентября',
    'октября',
    'ноября',
    'декабря',
  );
  ...
if ($block_name == 'pref_mister') {
    $result = db_query("
select r.id,
        count(r.id),
        u.first_name,
        u.avatar,
        u.city
from pref_rep r, pref_users u where
        r.nice=true and
        to_char(current_timestamp - interval '1 month', 'IYYY-MM') =
        to_char(r.last_rated, 'IYYY-MM') and
        u.female=false and
        r.id=u.id
group by r.id , u.first_name, u.avatar, u.city
order by count(r.id) desc
limit 1
");
    $record = $result->fetchObject();

    return array(
      'subject' => sprintf('Фаворит %s', $MONTHS[2]),
      'content' => sprintf('
<p align="center">%s</p>
<p align="center">%s</p>
<p align="center">%u оценок</p>',
                     user_link($record),
                     user_avatar($record),
                     $record->count),
    );
  }

I tried

setlocale(LC_TIME, "ru_RU");
print strftime("%B", strtotime("1/3/2003"));
# don't know how to get the prev. month date above...

but it has given me funny characters (probably not Russian in UTF8 - which is what I need).


date("n");

will give you the month without leading zeroes, subtract one for your array index.

Edit: I mean subtract 2, since you'll want the previous month. Then make sure it doesn't go into negatives.


Make a string array of months, (0 will be january, december will be 11) , get current month using a date function - if PHP has one.

Then search your string array for the index of the current month - 1 and return that

0

精彩评论

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