Exemplo n.º 1
0
function wp_get_pcalendar($args = '')
{
    global $wpdb, $m, $monthnum, $year, $timedifference, $month, $day, $posts;
    if (!empty($m)) {
        $m = preg_replace("/[^0-9]/", "", $m);
        $jy = substr($m, 0, 4);
    } elseif (!empty($year)) {
        $jy = $year;
    }
    if ($jy > 1500) {
        get_calendar();
    } else {
        $pd = bn_parsidate::getInstance();
        if (!$posts) {
            $gotsome = $wpdb->get_var("SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
            if (!$gotsome) {
                return;
            }
        }
        $week_begins = intval(get_option('start_of_week'));
        $w = isset($_GET['w']) ? intval($_GET['w']) : '';
        $is_gregorian = false;
        if (!empty($monthnum) && !empty($year)) {
            $thismonth = '' . zeroise(intval($monthnum), 2);
            $thisyear = '' . intval($year);
        } elseif (!empty($w)) {
            $thisyear = '' . intval(substr($m, 0, 4));
            $d = ($w - 1) * 7 + 6;
            //it seems MySQL's weeks disagree with PHP's
            $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')");
        } elseif (!empty($m)) {
            $thisyear = '' . intval(substr($m, 0, 4));
            if (strlen($m) < 6) {
                $thismonth = '01';
            } else {
                $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2);
            }
        } else {
            $is_gregorian = true;
            $thisyear = gmdate('Y', current_time('timestamp') + get_option('gmt_offset') * 3600);
            $thismonth = gmdate('m', current_time('timestamp') + get_option('gmt_offset') * 3600);
            $thisday = gmdate('d', current_time('timestamp') + get_option('gmt_offset') * 3600);
        }
        if ($is_gregorian) {
            list($jthisyear, $jthismonth, $jthisday) = $pd->gregorian_to_persian($thisyear, $thismonth, $thisday);
            $unixmonth = $pd->gregurian_date('Y-m-d 00:00:00', "{$jthisyear}-{$jthismonth}-01");
        } else {
            $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear);
            $jthisyear = $thisyear;
            $jthismonth = $thismonth;
        }
    }
    $jnextmonth = $jthismonth + 1;
    $jnextyear = $jthisyear;
    if ($jnextmonth > 12) {
        $jnextmonth = 1;
        $jnextyear++;
    }
    $start = $pd->gregurian_date('Y-m-d 00:00:00', "{$jthisyear}-{$jthismonth}-01");
    $end = $pd->gregurian_date('Y-m-d 00:00:00', "{$jnextyear}-{$jnextmonth}-01");
    $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t                FROM {$wpdb->posts}\n\t                WHERE post_date < '{$start}'\n\t                AND post_type = 'post' AND post_status = 'publish'\n                    ORDER BY post_date DESC\n                    LIMIT 1");
    $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t                FROM {$wpdb->posts}\n\t                WHERE post_date >= '{$end}'\n\t                AND post_type = 'post' AND post_status = 'publish'\n                    ORDER BY post_date ASC\n                    LIMIT 1");
    $calendar_output .= '<table id="wp-calendar" style="direction: rtl">
        <caption>' . $pd->persian_month_names[(int) $jthismonth] . ' ' . $pd->persian_date('Y', $unixmonth) . '</caption>
        <thead>
        <tr>';
    $myweek = array();
    for ($wdcount = 0; $wdcount <= 6; $wdcount++) {
        $myweek[] = $pd->persian_day_small[($wdcount + $week_begins) % 7];
    }
    foreach ($myweek as $wd) {
        $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$wd}</th>";
    }
    $calendar_output .= '
        </tr>
        </thead>
	
        <tfoot>
	    <tr>';
    if ($previous) {
        $previous_month = $jthismonth - 1;
        $previous_year = $jthisyear;
        if ($previous_month == 0) {
            $previous_month = 12;
            $previous_year--;
        }
        $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($previous_year, $previous_month) . '">&laquo; ' . $pd->persian_month_names[$previous_month] . '</a></td>';
    } else {
        $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad">&nbsp;</td>';
    }
    $calendar_output .= "\n\t\t" . '<td class="pad">&nbsp;</td>';
    if ($next) {
        $next_month = $jthismonth + 1;
        $next_year = $jthisyear;
        if ($next_month == 13) {
            $next_month = 1;
            $next_year++;
        }
        $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($next_year, $next_month) . '">' . $pd->persian_month_names[$next_month] . ' &raquo;</a></td>';
    } else {
        $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad">&nbsp;</td>';
    }
    $calendar_output .= '
        </tr>
        </tfoot>
	
        <tbody>
        <tr>';
    //____________________________________________________________________________________________________________________________________
    $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date),MONTH(post_date),YEAR(post_date)\n        FROM {$wpdb->posts} \n       \tWHERE post_date > '{$start}' AND post_date < '{$end}'\n        AND post_status = 'publish'", ARRAY_N);
    if ($dayswithposts) {
        foreach ($dayswithposts as $daywith) {
            $daywithpost[] = $pd->persian_date('j', "{$daywith['2']}-{$daywith['1']}-{$daywith['0']}", 'eng');
        }
    } else {
        $daywithpost = array();
    }
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) {
        $ak_title_separator = "\n";
    } else {
        $ak_title_separator = ', ';
    }
    $ak_titles_for_day = array();
    $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom, MONTH(post_date) as month, YEAR(post_date) as year \n        FROM {$wpdb->posts} WHERE post_date >= '{$start}' \n        AND post_date <= '{$end}' \n        AND post_type = 'post' AND post_status = 'publish'", ARRAY_N);
    if ($ak_post_titles) {
        foreach ($ak_post_titles as $ak_post_title) {
            /** This filter is documented in wp-includes/post-template.php */
            $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID));
            $ak_post_title[2] = $pd->persian_date('j', "{$ak_post_title['4']}-{$ak_post_title['3']}-{$ak_post_title['2']}", 'eng');
            if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) {
                $ak_titles_for_day['day_' . $ak_post_title->dom] = '';
            }
            if (empty($ak_titles_for_day[$ak_post_title->dom])) {
                // first one
                $ak_titles_for_day[$ak_post_title->dom] = $post_title;
            } else {
                $ak_titles_for_day[$ak_post_title->dom] .= $ak_title_separator . $post_title;
            }
        }
    }
    $pad = calendar_week_mod(date('w', strtotime($unixmonth)) - $week_begins);
    if (0 != $pad) {
        $calendar_output .= "\n\t\t" . '<td colspan="' . $pad . '" class="pad">&nbsp;</td>';
    }
    $daysinmonth = intval($pd->persian_date('t', $unixmonth, 'eng'));
    for ($day = 1; $day <= $daysinmonth; ++$day) {
        list($thiyear, $thismonth, $thisday) = $pd->persian_to_gregorian($jthisyear, $jthismonth, $day);
        if (isset($newrow) && $newrow) {
            $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
        }
        $newrow = false;
        if ($thisday == gmdate('j', time() + get_option('gmt_offset') * 3600) && $thismonth == gmdate('m', time() + get_option('gmt_offset') * 3600) && $thisyear == gmdate('Y', time() + get_option('gmt_offset') * 3600)) {
            $calendar_output .= '<td id="today">';
        } else {
            $calendar_output .= '<td>';
        }
        $p_day = empty($val['sep_datesnum']) ? $day : per_number($day);
        if (in_array($day, $daywithpost)) {
            // any posts today?
            $calendar_output .= '<a href="' . get_day_link($jthisyear, $jthismonth, $day) . "\" title=\"{$ak_titles_for_day[$day]}\">{$p_day}</a>";
        } else {
            $calendar_output .= $p_day;
        }
        $calendar_output .= '</td>';
        if (6 == calendar_week_mod($pd->gregurian_date('w', "{$jthisyear}-{$jthismonth}-{$day}") - $week_begins)) {
            $newrow = true;
        }
    }
    $pad = 7 - calendar_week_mod($pd->gregurian_date('w', "{$jthisyear}-{$jthismonth}-{$day}", 'eng') - $week_begins);
    if ($pad != 0 && $pad != 7) {
        $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . $pad . '">&nbsp;</td>';
    }
    echo $calendar_output . "\n\t</tr>\n\t</tbody>\n\t</table>";
}
Exemplo n.º 2
0
/**
 * gregdate()
 * convert persian datetime to gregorian datetime
 * 
 * @param mixed $input
 * @param mixed $datetime
 * @return datetime
 */
function gregdate($input, $datetime)
{
    $bndate = bn_parsidate::getInstance();
    $bndate = $bndate->gregurian_date($input, $datetime);
    return $bndate;
}