Results 1 to 6 of 6

Thread: Jalali date

  1. #1
    New user respinar's Avatar
    Join Date
    09-17-10.
    Location
    Tabriz, Iran
    Posts
    14

    Default Jalali date

    In Perisan (fa) we use Jalali date instead of Goerging, so, I need to parse Jalali date in contao. There is good converting functions, but, I don't know where it must used.

    I think, by using a extensions, I can do this. How an extenstions must be created to hack contao main date functions if the website language is fa?

    Please see blew code and help me.

    If this problem solved, contao usage will grow in persian countries.
    Code:
    <?php
    
    function div($a, $b) {
    	return (int) ($a / $b);
    }
    
    function gregorian_to_jalali($g_y, $g_m, $g_d)
    {
    
    	$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    	$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
    	$gy = $g_y - 1600;
    	$gm = $g_m - 1;
    	$gd = $g_d - 1;
    	$g_day_no = 365 * $gy + div($gy + 3,4) - div($gy + 99, 100) + div($gy + 399, 400);
    
    	for ($i=0 ; $i < $gm ; ++$i)
    	{
    		$g_day_no += $g_days_in_month[$i];
    	}
    
    	if ($gm>1 && (($gy % 4 == 0 && $gy % 100 != 0) || ($gy % 400 == 0)))
    	{
    		$g_day_no++; // leap and after Feb
    	}
    
    	$g_day_no += $gd;
    	$j_day_no = $g_day_no - 79;
    	$j_np = div($j_day_no, 12053); // 12053 = 365*33 + 32/4
    	$j_day_no = $j_day_no % 12053;
    	$jy = 979 + 33 * $j_np + 4 * div($j_day_no, 1461); // 1461 = 365*4 + 4/4
    	$j_day_no %= 1461;
    
    	if ($j_day_no >= 366)
    	{
    		$jy += div($j_day_no - 1, 365);
    		$j_day_no = ($j_day_no - 1) % 365;
    	}
    
    	for ($i = 0 ; $i < 11 && $j_day_no >= $j_days_in_month[$i] ; ++$i)
    	{
    		$j_day_no -= $j_days_in_month[$i];
    	}
    
    	$jm = $i + 1;
    	$jd = $j_day_no + 1;
    	return array($jy, $jm, $jd);
    }
    
    
    function tranasmonth($number) {
    	$month = array(
    		1	=> '???????',
    		2	=> '????????',
    		3	=> '?????',
    		4	=> '???',
    		5	=> '??????',
    		6	=> '??????',
    		7	=> '???',
    		8	=> '????',
    		9	=> '???',
    		10	=> '??',
    		11	=> '????',
    		12	=> '?????',
    	);
    	return strtr($number, $month);
    }
    
    function jalali_format($format, $date_trans, $zone_offset, $gmepoch)
    {
    	$part = explode(" ", @gmdate('Y m d', $gmepoch));
    	list( $jy, $jm, $jd ) = gregorian_to_jalali($part[0], $part[1], $part[2]);
    
    	$format = str_replace('Y', substr($jy, 2, 2), $format);// year as 2 digits
    	$format = str_replace('y', $jy, $format);// year as 4 digits
    	$format = str_replace('M', tranasmonth($jm), $format);// month as 3 lettersto letters in Persian
    	$format = str_replace('m', tranasmonth($jm), $format);// month as 2 digits to letters in Persian
    	$format = str_replace('F', tranasmonth($jm), $format);// month ,long form to letters in Persian
    	$format = str_replace('n', tranasmonth($jm), $format);// month as digits without leading zero to letters in Persian
    	$format = str_replace('d', $jd, $format);// day of the month with leading zero to digits without zero : 03 to 3
    	$format = str_replace('j', $jd, $format);// day of the month as digits without leading zero
    	$format = str_replace('S', ' ??', $format);// for translating English ordinal suffix as 2 character to Persian: "st, nd, rd, th" to " ??"
    
    	return strtr(@gmdate($format, $gmepoch + $zone_offset), $date_trans);
    }
    
    ?>
    Hamid Abbaszadeh
    https://respinar.com

  2. #2
    Community-Moderator xchs's Avatar
    Join Date
    06-19-09.
    Posts
    1,287

    Default Re: Jalali date

    Hi respinar,

    Quote Originally Posted by respinar
    In Perisan (fa) we use Jalali date instead of Goerging, so, I need to parse Jalali date in contao.
    Leo has accepted your feature request but I do actually not know when this will implemented: http://dev.contao.org/issues/2193
    Contao Community Moderator
    → Support options

  3. #3
    New user respinar's Avatar
    Join Date
    09-17-10.
    Location
    Tabriz, Iran
    Posts
    14

    Default Re: Jalali date

    I think it must be an extentsion, not in core, So by extending parseDate() functions by that function for when LANG is "fa":
    Code:
    if ($LANG == "fa") {
     parse Jalali date;
    } else {
     parse regular date;
    }
    This function must change all date that are shown in forentend.
    Hamid Abbaszadeh
    https://respinar.com

  4. #4
    New user respinar's Avatar
    Join Date
    09-17-10.
    Location
    Tabriz, Iran
    Posts
    14

    Default Overriding class methods or Hooks

    Hi,

    I develop a new class that can convert date to persian one. For using this class, I need to modify some core in system/modules/core/library/Contao/Date.php in parse function (line 580):

    Code:
                    if ($intTstamp === null)
    		{
    			if (TL_MODE == 'FE' && $GLOBALS['TL_LANGUAGE'] == 'fa') {
    				$strDate = PersianDate::date($strFormat,$intTstamp);
    			} else {
    				$strDate = date($strModified);
    			}
    		}
    		elseif (!is_numeric($intTstamp))
    		{
    			return '';
    		}
    		else
    		{
    			if (TL_MODE == 'FE' && $GLOBALS['TL_LANGUAGE'] == 'fa') {
    				$strDate = PersianDate::date($strFormat,$intTstamp);
    			} else {
    				$strDate = date($strModified);
    			}
    		}
    How can I use "Overriding class methods" or "Hooks" without modifying core?
    Attached Files Attached Files
    Last edited by respinar; 01/07/2014 at 09:02.
    Hamid Abbaszadeh
    https://respinar.com

  5. #5
    New user
    Join Date
    01-07-14.
    Posts
    5

    Default

    Hi,

    first you have to copy the contao default Date.php into your extension. Then you have to overwrite the default Date class in your autoload.php:
    PHP Code:
    'Contao\Date'                  => 'system/modules/persiandate/classes/MyDate.php'
    After that, contao should use your MyDate.php class.

  6. #6
    New user respinar's Avatar
    Join Date
    09-17-10.
    Location
    Tabriz, Iran
    Posts
    14

    Hi jk1,
    I did what you said but it did not work for me!

    I have a good code for my PersianDate class but I cannot understand how Contao/Date class work.

    Is it possible, you see my https://github.com/respinar/persiandate repo, and give me some more suggest?
    Hamid Abbaszadeh
    https://respinar.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •