Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#jDateTime

PHP class to convert dates from Gregorian calendar system to Jalali calendar system and vice versa. Supports dates beyond 2038.
Jalali, also known as Shamsi or Hijri Shamsi is the Iranian calendar system.
Jalali, also known as Shamsi or Hijri Shamsi is the Iranian calendar system.
It is also support Falaki month names.
[![Build Status](https://travis-ci.org/sallar/jDateTime.png?branch=master)](https://travis-ci.org/sallar/jDateTime)

#About v2.2.0
Expand Down Expand Up @@ -53,6 +54,7 @@ Please see [examples.php](examples.php) and [example-static.php](examples-static
- [Afshin Mehrabani](http://afshinm.name)
- [Amir Latifi](http://amiir.me)
- [Ruhollah Namjoo](https://github.com/namjoo)
- [Morteza Rajabi](https://github.com/mortezarajabi)

##License
jDateTime was created by [Sallar Kaboli](http://sallar.me) and released under the [MIT License](http://opensource.org/licenses/mit-license.php).
Expand Down
2 changes: 2 additions & 0 deletions examples-static.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

date_default_timezone_set('Asia/Tehran');

jDateTime::setMonthNameType('falaki');
echo jDateTime::date('l j F Y H:i');
echo "<br />";
echo jDateTime::date('Y-m-d', false, false);
echo "<br />";
echo jDateTime::date('Y-m-d', false, false, false);
echo "<br />";
jDateTime::setMonthNameType('jalali');
echo jDateTime::date("l j F Y H:i T", false, null, null, 'America/New_York');

//Just adding routine html tags.
Expand Down
5 changes: 4 additions & 1 deletion examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@

//Init
$date = new jDateTime(true, true, 'Asia/Tehran');

echo $date->date("l j F Y H:i"); // Outputs: پنجشنبه ۱۵ اردیبهشت ۱۳۹۰ ۰۰:۰۰
echo "<br />\n";
$date->setMonthNameType('falaki');// jalali or falaki
echo $date->date("l j F Y H:i"); // Outputs: پنجشنبه ۱۵ ثور ۱۳۹۰ ۰۰:۰۰
echo "<br />\n";
echo $date->date("Y-m-d", false, false); // Outputs: 1390-02-15
echo "<br />\n";
echo $date->date("Y-m-d", false, false, false); //Outputs: 2011-05-05
//Or you could just use: $date->gDate("Y-m-d");
//Same as above
echo "<br />\n";
$date->setMonthNameType('jalali');
echo $date->date("l j F Y H:i T", false, null, null, 'America/New_York'); //چهارشنبه ۱۴ اردیبهشت ۱۳۹۰ ۱۵:۳۰ EDT

echo "<br />\n";
Expand Down
23 changes: 20 additions & 3 deletions jdatetime.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class jDateTime
private static $convert = true; //Convert numbers to Farsi characters in utf-8
private static $timezone = null; //Timezone String e.g Asia/Tehran, Defaults to Server Timezone Settings
private static $temp = array();
private static $monthNameTypes = array('jalali', 'falaki'); //Available month name types
private static $monthNameType = 'jalali'; //Default month name type

/**
* jDateTime::Constructor
Expand All @@ -68,11 +70,25 @@ class jDateTime
*/
public function __construct($convert = null, $jalali = null, $timezone = null)
{
if ( $jalali !== null ) self::$jalali = (bool) $jalali;
if ( $convert !== null ) self::$convert = (bool) $convert;
if ( $jalali !== null ) self::$jalali = (bool) $jalali;
if ( $timezone !== null ) self::$timezone = $timezone;
}

/**
* jDateTime::setMonthNameType
*
* Pass jalali or falaki to change the month names
*
* @author Morteza Rajabi
* @param $type string Month name type
*/
public static function setMonthNameType($type = 'jalali')
{
if(in_array($type, self::$monthNameTypes))
self::$monthNameType = $type;
}

/**
* Convert a formatted string from Georgian Calendar to Jalali Calendar.
* This will be useful to directly convert time strings coming from databases.
Expand Down Expand Up @@ -484,9 +500,10 @@ private static function getMonthNames($month, $shorten = false, $len = 3)
{
// Convert
$months = array(
'فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'
'jalali' => ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'],
'falaki' => ['حمل', 'ثور', 'جوزا', 'سرطان', 'اسد', 'سنبله', 'میزان', 'عقرب', 'قوس', 'جدی', 'دلو', 'حوت']
);
$ret = $months[$month - 1];
$ret = $months[self::$monthNameType][$month - 1];

// Return
return ($shorten) ? self::substr($ret, 0, $len) : $ret;
Expand Down