add parse format

This commit is contained in:
nasser committed 2017-11-03 16:50:42 +03:30
1 parent ec3de390c0
commit 3405ab1714
3 files changed
+38 -2

No files matched your search

+2
View File
@@ -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 To use the jalali datetime as a string, use the following command
```php ```php
$v = Verta::parse('1395-10-07 14:12:32'); $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 If the date values are separate, use the following commands
+2
View File
@@ -88,6 +88,8 @@ return verta($c); // 1395-12-09 15:05:56
برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید
```php ```php
$v = Verta::parse('1395-10-07 14:12:32'); $v = Verta::parse('1395-10-07 14:12:32');
$v = Verta::parse('1396 مهر 17');
$v = Verta::parseFormat('Y n j','1396 مهر 17');
``` ```
در صورتی مقادیر تاریخ میلادی به صورت جداگانه باشد، از دستورات زیر استفاده کنید در صورتی مقادیر تاریخ میلادی به صورت جداگانه باشد، از دستورات زیر استفاده کنید
+34 -2
View File
@@ -344,11 +344,43 @@ class Verta extends DateTime {
* @return static * @return static
*/ */
public static function parse($datetime, $timezone = null) { 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){ if($parse['error_count'] == 0){
list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']); 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); $timezone = self::createTimeZone($timezone);
$datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone); $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone);