Skip to content

Commit

Permalink
update documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Morteza Parvini committed May 8, 2016
1 parent 585e060 commit 7f874b6
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 200 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 miladr
Copyright (c) 2016 Morilog

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
115 changes: 83 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
[![Build Status](https://travis-ci.org/morilog/jalali.svg?branch=master)](https://travis-ci.org/morilog/jalali)
morilog/jalali
======
- This package compatible with Laravel 5
- This package compatible with Laravel `>=5` & `< 6.0`

- This package was forked from [Miladr/Jalali](http://github.com/miladr/jalai) in previous version and fixed bugs and customized by [Morilog](http://morilog.ir)
- This package was forked from [Miladr/Jalali](http://github.com/miladr/jalai) in previous version and fixed bugs a

<a name="installation"></a>
## Installation
- Jalali calendar is a solar calendar that was used in Persia, variants of which today are still in use in Iran as well as Afghanistan. [Read more on Wikipedia](http://en.wikipedia.org/wiki/Jalali_calendar) or see [Calendar Converter](http://www.fourmilab.ch/documents/calendar/).

- Calendar conversion is based on the [algorithm provided by Kazimierz M. Borkowski](http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm) and has a very good performance.

- jDateTime class was ported from [jalaali/jalaali-js](https://github.com/jalaali/jalaali-js)

## Installation Version 2.0
> If you are using version < 2.0, please read [old docs](https://github.com/morilog/jalali/blob/v1.1/README.md)
Run the Composer update comand

Expand All @@ -17,41 +23,36 @@ In your `config/app.php` add `'Morilog\Jalali\JalaliServiceProvider'` to the end
```php
'providers' => [

'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
Illuminate\Auth\AuthServiceProvider::class,
...
'Morilog\Jalali\JalaliServiceProvider',
Morilog\Jalali\JalaliServiceProvider::class,

],
.
.
.
.
.


'alias' => [
...
'jDate' => 'Morilog\Jalali\Facades\jDate',
'jDateTime' => 'Morilog\Jalali\Facades\jDateTime',
'jDate' => Morilog\Jalali\Facades\jDate::class
]
```

<a name="basic-usage"></a>
## Basic Usage
## Examples ##

Some Examples (based on examples provided by Sallar)

### jDate
In version >= 1.1, You can use `jdate()` instead of `jDate::forge()`;

```php
#### `forge([$str = ''])`
``` php
// default timestamp is now
$date = jDate::forge();
// OR $date = jdate();
$date = \Morilog\Jalali\jDate::forge();
// OR
$date = jdate();

// pass timestamps
$date = jDate::forge(1333857600);
// OR $date = jdate(1333857600);

// OR
$date = jdate(1333857600);

// pass strings to make timestamps
$date = jDate::forge('last sunday');
Expand All @@ -71,15 +72,66 @@ $date = jDate::forge('last sunday')->format('time'); // 00:00:00
$date = jDate::forge('2012-10-12')->reforge('+ 3 days')->format('date'); // 1391-07-24

// get relative 'ago' format
$date = jDate::forge('now - 10 minutes')->ago() // ۱۰ دقیقه پیش
$date = jDate::forge('now - 10 minutes')->ago() // 10 دقیقه پیش
// OR
$date = jdate('now - 10 minutes')->ago() // 10 دقیقه پیش
```

### jDateTime
---


#### `checkDate($year, $month, $day, [$isJalali = true])`
```php
// Check jalali date
\Morilog\Jalali\jDateTime::checkDate(1391, 2, 30, true); // true

// Check jalali date
\Morilog\Jalali\jDateTime::checkDate(2016, 5, 7); // false

//date_parse_from_format for jalali date
$date = jDate::parseFromFormat('Y/m/d', '1393/01/18');
echo $date['year']; //1393
echo $date['month']; //01
echo $date['day']; //18
// Check gregorian date
\Morilog\Jalali\jDateTime::checkDate(2016, 5, 7, false); // true
```
---
#### `toJalali($gYear, $gMonth, $gDay)`
```php
\Morilog\Jalali\jDateTime::toJalali(2016, 5, 7); // [1395, 2, 18]
```
---
#### `toGregorian($jYear, $jMonth, $jDay)`
```php
\Morilog\Jalali\jDateTime::toGregorian(1395, 2, 18); // [2016, 5, 7]
```
---
#### `strftime($format, [$timestamp = false])`
```php
jDateTime::strftime('Y-m-d', strtotime('2016-05-8'); // 1395-02-19
```
---
#### `createDateTimeFromFormat($format, $jalaiTimeString)`
```php
$jdate = '1394/11/25 15:00:00';

// get instance of \DateTime
$dateTime = \Morilog\Jalali\jDatetime::createDatetimeFromFormat('Y/m/d H:i:s', $jdate);

```
---
#### `createCarbonFromFormat($format, $jalaiTimeString)`
```php
$jdate = '1394/11/25 15:00:00';

// get instance of \Carbon\Carbon
$carbon = \Morilog\Jalali\jDatetime::createDatetimeFromFormat('Y/m/d H:i:s', $jdate);

```
---
#### `convertNumbers($string)`
```php
$date = \Morilog\Jalali\jDateTime::strftime('Y-m-d', strtotime('2016-05-8'); // 1395-02-19
\Morilog\Jalali\jDateTime::convertNumbers($date); // ۱۳۹۵-۰۲-۱۹
```
---
## Formatting ##

For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php).
Expand All @@ -89,7 +141,6 @@ For help in building your formats, checkout the [PHP strftime() docs](http://php
The class relies on ``strtotime()`` to make sense of your strings, and ``strftime()`` to make the format changes. Just always check the ``time()`` output to see if you get false timestamps... which means the class couldn't understand what you were telling it.

## License ##
- This bundle is created based on [Laravel-Date](https://github.com/swt83/laravel-date) by [Scott Travis](https://github.com/swt83) (MIT Licensed).
- This bundle is created based on [Laravel-Date](https://github.com/swt83/laravel-date) by [Scott Travis](https://github.com/swt83) (MIT Licensed).
- [Jalali (Shamsi) DateTime](https://github.com/sallar/jDateTime) class included in the package is created by [Sallar Kaboli](http://sallar.me) and is released under the MIT License.
- This package was created by [Milad Rey](http://milad.io) for Laravel < 5 and is released under the MIT License.
- - This package was created and modified by [Morteza Parvini](http://morilog.ir) for Laravel >= 5 and is released under the MIT License.
- This package was created and modified by [Morteza Parvini](http://morilog.ir) for Laravel >= 5 and is released under the MIT License.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"keywords": ["Laravel","Date","Datetime","Jalali", "Morilog"],
"require": {
"php": ">=5.5",
"illuminate/support": "^5.0"
"illuminate/support": "^5.0",
"nesbot/carbon": "^1.21"
},
"require-dev" : {
"phpunit/phpunit": "~4.0"
Expand Down
112 changes: 6 additions & 106 deletions src/jDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,16 @@ public function __construct($str = null)
/**
* @return int
*/
public function time()
public function getTime()
{
return $this->time;
}

/**
* @param string $str
* @param bool|false $convertNumbersToPersian
* @return bool|string
*/
public function format($str, $convertNumbersToPersian = false)
public function format($str)
{
// convert alias string
if (in_array($str, array_keys($this->formats))) {
Expand All @@ -94,7 +93,7 @@ public function format($str, $convertNumbersToPersian = false)

// if valid unix timestamp...
if ($this->time !== false) {
return jDateTime::strftime($str, $this->time, $convertNumbersToPersian);
return jDateTime::strftime($str, $this->time);
} else {
return false;
}
Expand Down Expand Up @@ -129,7 +128,7 @@ public function reforge($str)
public function ago()
{
$now = time();
$time = $this->time();
$time = $this->getTime();

// catch error
if (!$time) {
Expand Down Expand Up @@ -168,106 +167,7 @@ public function until()
{
return $this->ago();
}


/**
* @param $format
* @param $date
* @return array
*/
public static function parseFromFormat($format, $date)
{
// reverse engineer date formats
$keys = array(
'Y' => array('year', '\d{4}'),
'y' => array('year', '\d{2}'),
'm' => array('month', '\d{2}'),
'n' => array('month', '\d{1,2}'),
'M' => array('month', '[A-Z][a-z]{3}'),
'F' => array('month', '[A-Z][a-z]{2,8}'),
'd' => array('day', '\d{2}'),
'j' => array('day', '\d{1,2}'),
'D' => array('day', '[A-Z][a-z]{2}'),
'l' => array('day', '[A-Z][a-z]{6,9}'),
'u' => array('hour', '\d{1,6}'),
'h' => array('hour', '\d{2}'),
'H' => array('hour', '\d{2}'),
'g' => array('hour', '\d{1,2}'),
'G' => array('hour', '\d{1,2}'),
'i' => array('minute', '\d{2}'),
's' => array('second', '\d{2}'),
);

// convert format string to regex
$regex = '';
$chars = str_split($format);
foreach ($chars as $n => $char) {
$lastChar = isset($chars[$n - 1]) ? $chars[$n - 1] : '';
$skipCurrent = '\\' == $lastChar;
if (!$skipCurrent && isset($keys[$char])) {
$regex .= '(?P<' . $keys[$char][0] . '>' . $keys[$char][1] . ')';
} else {
if ('\\' == $char) {
$regex .= $char;
} else {
$regex .= preg_quote($char);
}
}
}

$dt = array();
$dt['error_count'] = 0;
// now try to match it
if (preg_match('#^' . $regex . '$#', $date, $dt)) {
foreach ($dt as $k => $v) {
if (is_int($k)) {
unset($dt[$k]);
}
}
if (!jDateTime::checkdate($dt['month'], $dt['day'], $dt['year'], false)) {
$dt['error_count'] = 1;
}
} else {
$dt['error_count'] = 1;
}
$dt['errors'] = array();
$dt['fraction'] = '';
$dt['warning_count'] = 0;
$dt['warnings'] = array();
$dt['is_localtime'] = 0;
$dt['zone_type'] = 0;
$dt['zone'] = 0;
$dt['is_dst'] = '';

if (strlen($dt['year']) == 2) {
$now = self::forge('now');
$x = $now->format('Y') - $now->format('y');
$dt['year'] += $x;
}

$dt['year'] = isset($dt['year']) ? (int)$dt['year'] : 0;
$dt['month'] = isset($dt['month']) ? (int)$dt['month'] : 0;
$dt['day'] = isset($dt['day']) ? (int)$dt['day'] : 0;
$dt['hour'] = isset($dt['hour']) ? (int)$dt['hour'] : 0;
$dt['minute'] = isset($dt['minute']) ? (int)$dt['minute'] : 0;
$dt['second'] = isset($dt['second']) ? (int)$dt['second'] : 0;

return $dt;
}

/**
* @param $format
* @param $str
* @return \DateTime
*/
public static function dateTimeFromFormat($format, $str)
{
$jd = new jDate();
$pd = $jd->parseFromFormat($format, $str);
$gd = jDateTime::toGregorian($pd['year'], $pd['month'], $pd['day']);
$date = new \DateTime();
$date->setDate($gd[0], $gd[1], $gd[2]);
$date->setTime($pd['hour'], $pd['minute'], $pd['second']);

return $date;
}

}
Loading

0 comments on commit 7f874b6

Please sign in to comment.