WordPress.org

Ready to get started?Download WordPress

Forums

How to get month NAME not number? (2 posts)

  1. parakeet
    Member
    Posted 7 years ago #

    With the following...

    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts
    FROM $wpdb->posts WHERE post_date !=
    '0000-00-00 00:00:00' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");

    ... which returns the following numbers: $month->year (ie. 2006), $month->month (ie. 9) and $month->posts ie. 236)...

    ***How can I make sure I can back the NAME of the month and not just the number?***

  2. Kafkaesqui

    Posted 7 years ago #

    First, the query line displayed above does *not* return anything in $month. You sort of stepped over the important part in your code.

    Second, I would not use $month in this way because what you're looking for is provided through the default WordPress array $month. For example:

    echo $month[02];

    would display "February". So let's say you change $month to $archives in that missing code of yours. You can then do something like:

    $thismonth = $month[zeroise($archives->month, 2)];

    (i.e. '9' is converted to 'September')

    Note: zeroise() is a WordPress function which just prepends 0's. The second argument ('2' above) is the final character length for the var, so the function decides how many 0's to add based on that. Useful since the $month array assigns 01 => 'January', 02 = > 'February', etc.

Topic Closed

This topic has been closed to new replies.

About this Topic