diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ac9168 --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ + +# Created by https://www.gitignore.io/api/phpstorm + +### PhpStorm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Composer +vendor + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### PhpStorm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr \ No newline at end of file diff --git a/.idea/EasyDateTime.iml b/.idea/EasyDateTime.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/EasyDateTime.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..ad691a4 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72abef0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b013172 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e38b541 --- /dev/null +++ b/README.md @@ -0,0 +1,84 @@ +#EasyDateTime + +EasyDateTime is a Jalali/Shamsi and Gregorian class based on jDateTimePlus that aims to simplify common DateTime tasks by abstracting the complex layers. It's the **simplest** solution for Jalali/Shamsi calendar issues in PHP. + +#About this class + +EasyDateTime is directly based on `jDateTimePlus` 1.0.0 which is itself based on `jDateTime` 2.2.0 + +#Requirements + +* **PHP >= 5.4**. +* Being based on jDateTimePlus, this package has a dependency on jDateTimePlus 1.0.0+ + +#Installation + +##Using Composer + +You can install this package using [composer](https://getcomposer.org). Add this package to your `composer.json`: + +``` +"require": { + "vsg24/easydatetime": "dev-master" +} +``` + +or if you prefer command line, change directory to project root and: + +``` +php composer.phar require "vsg24/easydatetime":"dev-master" +``` + +##Manual Installation + +Get a copy of package source code. You can do this in two ways: + +1. Download ZIP version of the source code and unzip it in desired location +2. Run `git clone https://github.com/vsg24/easydatetime.git` to clone this repository + +After getting a copy of source code, it is enough to include `easydatetime.class.php` where you need to use it. + +```php +require_once 'path/to/source/easydatetime.class.php'; +``` + +#Examples + +Please run [examples.php](examples.php) or see [examples-compiled.html](examples-compiled.html) for working examples. + +**Important Note:** +If you want to do more complicated things, you can access an instance of `jDatetimePlus` from an EasyDateTime object: + + `$edtObj->$jDatetimePlus` + +#Contributors: +- [Vahid Amiri Motlagh](http://atvsg.com) + +##License +EasyDateTime was created by [Vahid Amiri Motlagh](http://atvsg.com) and released under the [MIT License](http://opensource.org/licenses/mit-license.php). + +Copyright (C) 2016 [Vahid Amiri Motlagh](http://atvsg.com) + + + + The MIT License (MIT) + + Copyright (C) 2016 Vahid Amiri Motlagh + + 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 the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + 1- The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d1af0da --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "vsg24/easydatetime", + "type": "library", + "description": "Jalali (Shamsi) DateTime class written in PHP, abstraction, easy, little configuration", + "keywords": ["date", "datetime", "jalali", "jalalidate", "jdatetime","shamsi","iran","farsi","persian","time"], + "homepage": "https://vsg24.github.io/EasyDateTime/", + "license": "MIT", + "authors": [ + { + "name": "Vahid Amiri Motlagh", + "email": "vahid.a1996@gmail.com", + "homepage": "http://atvsg.com", + "role": "Developer" + } + ], + "require": { + "php": ">=5.4.0", + "vsg24/jdatetimeplus": ">=1.0.0" + }, + "autoload": { + "classmap": [ + "easydatetime.class.php" + ] + }, + "minimum-stability": "dev" +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..9af96f2 --- /dev/null +++ b/composer.lock @@ -0,0 +1,72 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "b0828931a10783bf88fb9e867f060472", + "content-hash": "e74ed28816885f561984c2c6d080092c", + "packages": [ + { + "name": "vsg24/jdatetimeplus", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/VSG24/jDateTimePlus.git", + "reference": "768ba50efe5e249bfa000690ae6287e70aef3013" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/VSG24/jDateTimePlus/zipball/768ba50efe5e249bfa000690ae6287e70aef3013", + "reference": "768ba50efe5e249bfa000690ae6287e70aef3013", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "jdatetimeplus.class.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Vahid Amiri Motlagh", + "email": "vahid.a1996@gmail.com", + "homepage": "http://atvsg.com", + "role": "Developer" + } + ], + "description": "Jalali (Shamsi) DateTime class written in PHP, Supports year higher than 2038. Updated in 2016.", + "homepage": "https://vsg24.github.io/jDateTimePlus/", + "keywords": [ + "Jalali", + "date", + "datetime", + "farsi", + "iran", + "jalalidate", + "jdatetime", + "persian", + "shamsi", + "time" + ], + "time": "2016-07-06 18:57:15" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.4.0" + }, + "platform-dev": [] +} diff --git a/easydatetime.class.php b/easydatetime.class.php new file mode 100644 index 0000000..7f4d1be --- /dev/null +++ b/easydatetime.class.php @@ -0,0 +1,199 @@ + + * http://atvsg.com + * + * The MIT License (MIT) + * + * 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 the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * 1- The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * List of supported timezones can be found here: + * http://www.php.net/manual/en/timezones.php + * + * + * @package EasyDateTime + * @author Vahid Amiri Motlagh + * @copyright 2016 Vahid Amiri Motlagh + * @license http://opensource.org/licenses/mit-license.php The MIT License + * @link https://github.com/vsg24/EasyDateTime + * @see DateTime + * @version 1.0.0 + */ +class EasyDateTime +{ + public $timezone; // any php standard timezone + public $mode; // 'jalali' or 'gregorian' + public $jDatetimePlus; // an instance of jDateTimePlus + + public function __construct($timezone, $mode) + { + $this->timezone = $timezone; + $this->mode = $mode; + if($mode == 'jalali') + { + $this->jDatetimePlus = new jDateTimePlus(false, true, $timezone); + } + else + { + $this->jDatetimePlus = new jDateTimePlus(false, false, $timezone); + } + } + + /** + * Formats and returns given timestamp just like php's + * built in date() function. + * + * @param string $format + * @param string|bool $timestamp + * @return string + */ + public function date($format = 'Y-m-d H:i:s', $timestamp = false) + { + if($this->mode == 'jalali') + { + return $this->jDatetimePlus->date($format, $timestamp, false, true, $this->timezone); + } + else + { + return $this->jDatetimePlus->date($format, $timestamp, false, false, $this->timezone); + } + } + + /** + * Converts a given Gregorian date string to the equivalent Jalali date string. + * + * @param string $jalaliFormat + * @param string $gregorianFormat + * @param string $datetimeString + * @return \Exception|string + */ + public function gregorianStringToJalaliString($datetimeString, $gregorianFormat, $jalaliFormat) + { + if($this->mode == 'jalali') + { + return $this->jDatetimePlus->convertFormatToFormat($jalaliFormat, $gregorianFormat, $datetimeString, + $this->timezone, false); + } + return new \Exception("self::mode should be 'jalali' for gregorianStringToJalaliString() method to work + properly."); + } + + /** + * Converts a given Jalali DateTime string to a formatted Gregorian DateTime + * Supported formats for Jalali input include: + * + * 1395-04-16 19:25:15 + * 1395/04/16 19:25:15 + * 1395-04-16 19:25 + * 1395/04/16 19:25 + * 1395-04-16 + * 1395/04/16 + * + * @param string $datetimeString + * @param string $gregorianFormat + * @return string + */ + public function jalaliStringToGregorianString($datetimeString, $gregorianFormat = 'Y-m-d H:i:s') + { + $datetime_sep = explode(' ', $datetimeString); + $date_sep1 = explode('-', $datetime_sep[0]); + $date_sep2 = explode('/', $datetime_sep[0]); + + $datetimeInfo = []; + + if(count($datetime_sep) === 2) + { + // contains time + $time = $datetime_sep[1]; + $time_sep = explode(':', $time); + + if(count($time_sep) === 2) { + // does not contain seconds + $datetimeInfo['hour'] = $time_sep[0]; + $datetimeInfo['minute'] = $time_sep[1]; + $datetimeInfo['second'] = 0; + } elseif(count($time_sep) === 3) { + // contains seconds + $datetimeInfo['hour'] = $time_sep[0]; + $datetimeInfo['minute'] = $time_sep[1]; + $datetimeInfo['second'] = $time_sep[2]; + } + } else { + $datetimeInfo['hour'] = 0; + $datetimeInfo['minute'] = 0; + $datetimeInfo['second'] = 0; + } + + if(count($date_sep1) === 3 || count($date_sep2) == 3) + { + // supported + $datetimeInfo['year'] = (isset($date_sep1[0]) && count($date_sep1[0]) === 4) ? $date_sep1[0] : $date_sep2[0]; + $datetimeInfo['month'] = isset($date_sep1[1]) ? $date_sep1[1] : $date_sep2[1]; + $datetimeInfo['day'] = isset($date_sep1[2]) ? $date_sep1[2] : $date_sep2[2]; + } + + $timestamp = $this->jDatetimePlus->mktime($datetimeInfo['hour'], $datetimeInfo['minute'], + $datetimeInfo['second'], $datetimeInfo['month'], $datetimeInfo['day'], $datetimeInfo['year'], true); + + return $this->jDatetimePlus->gDate($gregorianFormat, $timestamp); + } + + /** + * Converts a DateTime string from the given format and timezone to the field timezone and passed format + * + * @param $gregorianInputDateTime + * @param $inputTimezone + * @param $outputTimezone + * @param string $outputFormat + * @return string + */ + public function convertTimeZone($gregorianInputDateTime, $inputTimezone, $outputTimezone, $outputFormat = 'Y-m-d H:i:s') + { + $original_timezone = new DateTimeZone($inputTimezone); + // Instantiate the DateTime object, setting it's date, time and time zone. + $datetime = new DateTime($gregorianInputDateTime, $original_timezone); + + // Set the DateTime object's time zone to convert the time appropriately. + $target_timezone = new DateTimeZone($outputTimezone); + $datetime->setTimezone($target_timezone); + + // Outputs a date/time string based on the time zone you've set on the object. + $output = $datetime->format($outputFormat); + + return $output; + } + + /** + * Converts numbers in a given string from English to Persian and vice versa + * + * @param $string + * @return string + */ + public function convertNumbers($string) + { + return $this->jDatetimePlus->convertNumbers($string, $this->mode == 'jalali' ? 'fa' : 'en'); + } +} \ No newline at end of file diff --git a/examples-compiled.html b/examples-compiled.html new file mode 100644 index 0000000..9ce6e23 --- /dev/null +++ b/examples-compiled.html @@ -0,0 +1,45 @@ + + + + + EasyDateTime examples + + + +

+ EasyDateTime examples v1.0.0

+
+ +All input dates are expected in the UTC format, the methods will try to convert them into the timezone of +the object. If you don't want this behaviour you need to set the timezone property to 'UTC'.
$edt = new EasyDateTime('Asia/Tehran', 'jalali');
$edt->date();
Gets the current datetime - no need for any parameters!

1395-04-17 06:09:42
$edt->date('d l M');

17 پنجشنبه تیر
$edt->date('Y-m-d H:i:s', 1467851310);
Timestamp is in UTC, but the function +converts it to the current EasyDateTime object's timezone.

1395-04-17 04:58:30
$edt->convertNumbers($edt->date());

۱۳۹۵-۰۴-۱۷ ۰۶:۰۹:۴۲
$edt->timezone = 'America/Los_Angeles';
Changing timezone can be done +easily at runtime

1395-04-16 18:39:42
$edt->gregorianStringToJalaliString('2016-07-07 00:45', 'Y-m-d H:i', 'Y-m-d H:i'); +
Converting Gregorian date-times to Jalali, is a trivial task

1395-04-17 00:45
$edt->jalaliStringToGregorianString('1395-04-17 01:14', 'Y-m-d H:i'); +
The opposite (Jalali to Gregorian) is as easy, though more limited (Read the phpdocs for the method) +

2016-07-07 05:44
$edt->convertTimeZone('2016-07-07 00:00:00', 'America/Los_Angeles', 'Asia/Tehran', 'Y/m/d +H:i:s');
Converting between timezones is easy and doesn'n depend on the timezone of the EasyDateTime +object

2016/07/07 11:30:00
+ + + \ No newline at end of file diff --git a/examples.php b/examples.php new file mode 100644 index 0000000..7dc6021 --- /dev/null +++ b/examples.php @@ -0,0 +1,105 @@ + + + + + + EasyDateTime examples + + + +

+ EasyDateTime examples v1.0.0

+
+ +UTC format, the methods will try to convert them into the timezone of +the object. If you don\'t want this behaviour you need to set the timezone property to \'UTC\'.'; + +echo '
'; + +// Instantiate +echo '$edt = new EasyDateTime(\'Asia/Tehran\', \'jalali\');'; +$edt = new EasyDateTime('Asia/Tehran', 'jalali'); + + +echo '
'; + +echo '$edt->date();
Gets the current datetime - no need for any parameters!

'; +echo $edt->date(); + +echo '
'; + +echo '$edt->date(\'d l M\');

'; +echo $edt->date('d l M'); + +echo '
'; + +echo '$edt->date(\'Y-m-d H:i:s\', 1467851310);
Timestamp is in UTC, but the function +converts it to the current EasyDateTime object\'s timezone.

'; +echo $edt->date('Y-m-d H:i:s', 1467851310); + +echo '
'; + +echo '$edt->convertNumbers($edt->date());

'; +echo $edt->convertNumbers($edt->date()); + +echo '
'; + +echo '$edt->timezone = \'America/Los_Angeles\';
Changing timezone can be done +easily at runtime

'; +$edt->timezone = 'America/Los_Angeles'; +echo $edt->date(); + +echo '
'; + +echo '$edt->gregorianStringToJalaliString(\'2016-07-07 00:45\', \'Y-m-d H:i\', \'Y-m-d H:i\'); +
Converting Gregorian date-times to Jalali, is a trivial task

'; +$edt->timezone = 'Asia/Tehran'; +echo $edt->gregorianStringToJalaliString('2016-07-07 00:45', 'Y-m-d H:i', 'Y-m-d H:i'); + +echo '
'; + +echo '$edt->jalaliStringToGregorianString(\'1395-04-17 01:14\', \'Y-m-d H:i\'); +
The opposite (Jalali to Gregorian) is as easy, though more limited (Read the phpdocs for the method) +

'; +echo $edt->jalaliStringToGregorianString('1395-04-17 01:14', 'Y-m-d H:i'); + +echo '
'; + +echo '$edt->convertTimeZone(\'2016-07-07 00:00:00\', \'America/Los_Angeles\', \'Asia/Tehran\', \'Y/m/d +H:i:s\');
Converting between timezones is easy and doesn\'n depend on the timezone of the EasyDateTime +object

'; +echo $edt->convertTimeZone('2016-07-07 00:00:00', 'America/Los_Angeles', 'Asia/Tehran', 'Y/m/d H:i:s'); + +echo '
'; + + +?> + + +