<?php
/**
 * @author: Reza Gholampour
 * Website: http://www.phps.ir
 * Email: info@phps.ir
 */

function gregorian_to_jalali($gy, $gm, $gd, $mod = '') {
    $g_d_m = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
    $gy2 = ($gm > 2) ? ($gy + 1) : $gy;
    $days = 355666 + (365 * $gy) + (int)(($gy2 + 3) / 4) - (int)(($gy2 + 99) / 100) + (int)(($gy2 + 399) / 400) + $gd + $g_d_m[$gm - 1];
    $days = $days - 226899;
    $jz = (int)(($days + 6) / 2820);
    $jy = (int)((($days + 6) - ($jz * 2820)) * 100 / 36525 + 1);
    $jm = (int)((($days + 6) - ($jz * 2820)) - ((int)(($jy - 1) * 365.25)));
    $jd = $jm;
    $jm = (int)(($jm + 2) / 31);
    $jd = $jd - (int)(($jm - 1) * 31) + 1;
    return array($jy, $jm, $jd);
}

function jdate($format, $timestamp = '') {
    if ($timestamp == '') {
        $timestamp = time();
    }
    $timestamp = (int)$timestamp;
    $date = getdate($timestamp);
    list($jy, $jm, $jd) = gregorian_to_jalali($date['year'], $date['mon'], $date['mday']);
    $months = array('', 'فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند');
    $weekdays = array('یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنج شنبه', 'جمعه', 'شنبه');
    $format = str_replace('Y', $jy, $format);
    $format = str_replace('n', $jm, $format);
    $format = str_replace('j', $jd, $format);
    $format = str_replace('F', $months[$jm], $format);
    $format = str_replace('l', $weekdays[$date['wday']], $format);
    $format = str_replace('H', str_pad($date['hours'], 2, '0', STR_PAD_LEFT), $format);
    $format = str_replace('i', str_pad($date['minutes'], 2, '0', STR_PAD_LEFT), $format);
    $format = str_replace('s', str_pad($date['seconds'], 2, '0', STR_PAD_LEFT), $format);
    return $format;
}
?>