From 9b07e845055765e3a2563ebf32b3f02da796274c Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Wed, 1 Mar 2017 00:05:56 +0330 Subject: [PATCH] add predefine format and now method --- README.md | 24 ++++++++++++++--- src/Verta.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4b5bac3..61e3e24 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ You can use verta ``` php // default timestamp is now $dt = new Verta(); +//OR +$dt = Verta::now(); +//OR +$dt = Verta::instance('2016-12-25 11:12:36'); // pass timestamps $dt = new Verta(1333857600); @@ -55,18 +59,30 @@ return new Verta($dt); // 1395-12-09 15:05:56 $c = \Carbon::now(); return new Verta($c); // 1395-12-09 15:05:56 -// access static -return Verta::instance('2016-12-25 11:12:36'); //1395-10-05 11:12:36 - // pass strings persian date $dt = Verta::parse('1395-10-07 14:12:32'); // format the timestamp $dt = new Verta('2016-12-27 14:12:32'); -return $dt->format('Y-n-j H:i'); //1395-10-7 14:12 +return $dt->format('Y-n-j H:i'); // 1395-10-7 14:12 return $dt->format('%B %d، %Y'); // دی 07، 1395 return $dt; //1395-10-07 14:12:32 +// use predefined format +$dt = new Verta(); +return $dt->format('datetime'); // 1395-12-10 23:25:12 +return $dt->format('date'); // 1395-12-10 +return $dt->format('time'); // 23:26:35 + +// use predefined format method +$dt = new Verta(); +return $dt->formatDatetime(); // 1395-12-10 23:37:26 +return $dt->formatDate(); // 1395-12-10 +return $dt->formatTime(); // 23:26:35 +return $dt->formatPersianDatetime(); // 1395/12/10 23:46:09 +return $dt->formatPersianDate(); // 1395/12/10 + + // get a diffrent time with now $dt = Verta::parse('1395-10-07 14:12:32'); return $dt->diffrentNow(); // 2 ماه پیش diff --git a/src/Verta.php b/src/Verta.php index 726d8c8..26be5e6 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -239,6 +239,15 @@ class Verta extends DateTime { self::ENGLISH_NINE, ); + /** + * @var array + */ + protected static $formats = array( + 'datetime' => 'Y-m-d H:i:s', + 'date' => 'Y-m-d', + 'time' => 'H:i:s', + ); + /** * Format to use for __toString method when type juggling occurs. * @@ -281,10 +290,21 @@ class Verta extends DateTime { parent::__construct(date('Y-m-d H:i:s.u', $instance), $timezone); } + /** + * Create a Jalali now Datetime + * + * + * @return static + */ + public static function now() { + return new static(); + } + /** * Create a Jalali instance from a DateTime one * - * @param DateTime $datetime + * @param timestamp $timestamp [optional] + * @param bool $timezone [optional] * * @return static */ @@ -614,9 +634,62 @@ class Verta extends DateTime { */ public function format($format) { + if (in_array($format, array_keys(self::$formats))) { + $format = self::$formats[$format]; + } return $this->strftime($format); } + /** + * Format the instance as date + * + * @return string + */ + public function formatDatetime() + { + return $this->format('Y-m-d H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatDate() + { + return $this->format('Y-m-d'); + } + + /** + * Format the instance as time + * + * @return string + */ + public function formatTime() + { + return $this->format('H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatPersianDatetime() + { + return $this->format('Y/n/j H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatPersianDate() + { + return $this->format('Y/n/j'); + } + /** * get difference to now