From 3405ab1714d25138eb9bdeb61f6073bdf792e359 Mon Sep 17 00:00:00 2001 From: nasser Date: Fri, 3 Nov 2017 16:50:42 +0330 Subject: [PATCH] add parse format --- README.en.md | 2 ++ README.md | 2 ++ src/Verta.php | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.en.md b/README.en.md index c412942..0dd446f 100644 --- a/README.en.md +++ b/README.en.md @@ -89,6 +89,8 @@ return verta($c); // 1395-12-09 15:05:56 To use the jalali datetime as a string, use the following command ```php $v = Verta::parse('1395-10-07 14:12:32'); +$v = Verta::parse('1396 مهر 17'); +$v = Verta::parseFormat('Y n j','1396 مهر 17'); ``` If the date values are separate, use the following commands diff --git a/README.md b/README.md index b9950a0..6275c1b 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ return verta($c); // 1395-12-09 15:05:56 برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید ```php $v = Verta::parse('1395-10-07 14:12:32'); +$v = Verta::parse('1396 مهر 17'); +$v = Verta::parseFormat('Y n j','1396 مهر 17'); ``` در صورتی مقادیر تاریخ میلادی به صورت جداگانه باشد، از دستورات زیر استفاده کنید diff --git a/src/Verta.php b/src/Verta.php index 03cb82c..927fb65 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -344,11 +344,43 @@ class Verta extends DateTime { * @return static */ public static function parse($datetime, $timezone = null) { - $parse = date_parse($datetime); + $monthName = array_map(function ($value) { + return ' ' . $value . ' '; + }, self::$monthYear); + $monthValue = array_map(function ($value) { + return '-' . $value . '-'; + }, range(1,12)); + $datetime = str_replace($monthName, $monthValue, $datetime); + $parse = date_parse($datetime); if($parse['error_count'] == 0){ list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']); - list( $hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); + list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); + + $timezone = self::createTimeZone($timezone); + $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone); + return new static($datetime); + } + else{ + throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); + } + } + + /** + * Create a Verta instance from a DateTime one + * + * @param string $format + * @param string $datetime [optional] + * @param bool $timezone [optional] + * @return static + */ + public static function parseFormat($format,$datetime, $timezone = null) { + $datetime = str_replace(self::$monthYear, range(1,12), $datetime); + + $parse = date_parse_from_format($format, $datetime); + if($parse['error_count'] == 0){ + list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']); + list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); $timezone = self::createTimeZone($timezone); $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone);