Skip to content

Instantly share code, notes, and snippets.

@raideus
Created April 30, 2013 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raideus/5485853 to your computer and use it in GitHub Desktop.
Save raideus/5485853 to your computer and use it in GitHub Desktop.
<?php
/**
* Returns an array of dates in which content has been published
*
* @param string $date_type 'day', 'month', or 'year'
* @param string $format PHP date format string
* @param string $post_type Post type to get dates for
* @return array Array of dates in which content has been published
*/
function get_dates($date_type = 'year', $post_type = 'post', $format = false ) {
$display_format = "Y";
$compare_format = "Y";
if ($date_type == 'month') {
$display_format = "M Y";
$compare_format = "Y-m";
} else if ($date_type == 'day') {
$display_format = "M j, Y";
$compare_format = "Y-m-d";
}
if ($format) $display_format = $format;
$posts = get_posts(array('numberposts' => -1, 'post_type' => $post_type));
$previous_display = "";
$previous_value = "";
$count = 0;
$dates = array();
foreach($posts as $post) {
$post_date = strtotime($post->post_date);
$current_display = date_i18n($display_format, $post_date);
$current_value = date($compare_format, $post_date);
if ($previous_value != $current_value) {
$dates[$current_value] = $current_display;
}
$previous_display = $current_display;
$previous_value = $current_value;
}
return $dates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment