Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Date.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * คลาสจัดการเกี่ยวกับวันที่และเวลา
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Date
 21: {
 22:     /**
 23:      * @var mixed
 24:      */
 25:     private static $lang;
 26: 
 27:     /**
 28:      * class constructer
 29:      */
 30:     public function __construct()
 31:     {
 32:         self::$lang = Language::getItems(array(
 33:             'DATE_SHORT',
 34:             'DATE_LONG',
 35:             'MONTH_SHORT',
 36:             'MONTH_LONG',
 37:             'YEAR_OFFSET',
 38:         ));
 39:     }
 40: 
 41:     /**
 42:      * ฟังก์ชั่น คำนวนความแตกต่างของวัน (เช่น อายุ)
 43:      * คืนค่า จำนวนวัน(ติดลบได้) ปี เดือน วัน [days, year, month, day] ที่แตกต่าง.
 44:      *
 45:      * @assert (mktime(0, 0, 0, 2, 1, 2016), mktime(0, 0, 0, 3, 1, 2016)) [==]  array('days' => 29, 'year' => 0,'month' => 1, 'day' => 0)
 46:      * @assert ('2016-3-1', '2016-2-1') [==]  array('days' => -29, 'year' => 0,'month' => 1, 'day' => 0)
 47:      *
 48:      * @param string|int  $begin_date วันที่เริ่มต้นหรือวันเกิด (Unix timestamp หรือ วันที่ รูปแบบ YYYY-m-d)
 49:      * @param istring|int $end_date   วันที่สิ้นสุดหรือวันนี้ (Unix timestamp หรือ วันที่ รูปแบบ YYYY-m-d)
 50:      *
 51:      * @return array
 52:      */
 53:     public static function compare($begin_date, $end_date)
 54:     {
 55:         if (is_string($begin_date) && preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $begin_date, $match)) {
 56:             $begin_date = mktime(0, 0, 0, (int) $match[2], (int) $match[3], (int) $match[1]);
 57:         }
 58:         if (is_string($end_date) && preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $end_date, $match)) {
 59:             $end_date = mktime(0, 0, 0, (int) $match[2], (int) $match[3], (int) $match[1]);
 60:         }
 61:         if ($end_date == $begin_date) {
 62:             // เท่ากัน
 63:             return array(
 64:                 'days' => 0,
 65:                 'year' => 0,
 66:                 'month' => 0,
 67:                 'day' => 0,
 68:             );
 69:         } else {
 70:             // จำนวนวันที่แตกต่าง
 71:             $days = floor(($end_date - $begin_date) / 86400);
 72:             if ($end_date < $begin_date) {
 73:                 $tmp = $begin_date;
 74:                 $begin_date = $end_date;
 75:                 $end_date = $tmp;
 76:             }
 77:         }
 78:         $Year1 = (int) date('Y', $begin_date);
 79:         $Month1 = (int) date('m', $begin_date);
 80:         $Day1 = (int) date('d', $begin_date);
 81:         $Year2 = (int) date('Y', $end_date);
 82:         $Month2 = (int) date('m', $end_date);
 83:         $Day2 = (int) date('d', $end_date);
 84:         // วันแต่ละเดือน
 85:         $months = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 86:         // ปีอธิกสุรทิน
 87:         if (($Year2 % 4) == 0) {
 88:             $months[2] = 29;
 89:         }
 90:         // ปีอธิกสุรทิน
 91:         if ((($Year2 % 100) == 0) & (($Year2 % 400) != 0)) {
 92:             $months[2] = 28;
 93:         }
 94:         if (abs($days) < $months[$Month1]) {
 95:             // ไม่เกิน 1 เดือน
 96:             return array(
 97:                 'days' => $days,
 98:                 'year' => 0,
 99:                 'month' => 0,
100:                 'day' => abs($days),
101:             );
102:         } else {
103:             // ห่างกันเกิน 1 เดือน
104:             $YearDiff = $Year2 - $Year1;
105:             if ($Month2 >= $Month1) {
106:                 $MonthDiff = $Month2 - $Month1;
107:             } else {
108:                 --$YearDiff;
109:                 $MonthDiff = 12 + $Month2 - $Month1;
110:             }
111:             if ($Day1 > $months[$Month2]) {
112:                 $Day1 = 0;
113:             } elseif ($Day1 > $Day2) {
114:                 $Month2 = $Month2 == 1 ? 13 : $Month2;
115:                 $Day2 += $months[$Month2 - 1];
116:                 --$MonthDiff;
117:             }
118:             return array(
119:                 'days' => $days,
120:                 'year' => $YearDiff,
121:                 'month' => $MonthDiff,
122:                 'day' => $Day2 - $Day1,
123:             );
124:         }
125:     }
126: 
127:     /**
128:      * คืนค่าเวลาที่แตกต่าง หน่วย msec
129:      *
130:      * @assert ('08:00', '09:00') [==] 3600
131:      *
132:      * @param  $firstTime
133:      * @param  $lastTime
134:      *
135:      * @return int
136:      */
137:     public static function timeDiff($firstTime, $lastTime)
138:     {
139:         $firstTime = strtotime($firstTime);
140:         $lastTime = strtotime($lastTime);
141:         $timeDiff = $lastTime - $firstTime;
142:         return $timeDiff;
143:     }
144: 
145:     /**
146:      * แปลงตัวเลขเป็นชื่อวันตามภาษาที่ใช้งานอยู่
147:      * คืนค่า อาทิตย์...6 เสาร์.
148:      *
149:      * @assert (0) [==] 'อา.'
150:      * @assert (0, false) [==] 'อาทิตย์'
151:      *
152:      * @param int  $date       0-6
153:      * @param bool $short_date true (default) วันที่แบบสั้น เช่น อ., false ชื่อเดือนแบบเต็ม เช่น อาทิตย์
154:      *
155:      * @return string
156:      */
157:     public static function dateName($date, $short_date = true)
158:     {
159:         // create class
160:         if (!isset(self::$lang)) {
161:             new static();
162:         }
163:         $var = $short_date ? self::$lang['DATE_SHORT'] : self::$lang['DATE_LONG'];
164:         return isset($var[$date]) ? $var[$date] : '';
165:     }
166: 
167:     /**
168:      * ฟังก์ชั่นแปลงเวลาเป็นวันที่ตามรูปแบบที่กำหนด สามารถคืนค่าวันเดือนปี พศ. ได้ ขึ้นกับไฟล์ภาษา
169:      * คืนค่า วันที่และเวลาตามรูปแบบที่กำหนดโดย $format
170:      *
171:      * @assert (0, 'y-m-d H:i:s') [==]  date('y-m-d H:i:s')
172:      * @assert (null) [==]  ''
173:      * @assert (1454259600, 'Y-m-d H:i:s') [==] '2559-02-01 00:00:00'
174:      *
175:      * @param int|string $time   int เวลารูปแบบ Unix timestamp, string เวลารูปแบบ Y-m-d หรือ Y-m-d H:i:s ถ้าไม่ระบุหรือระบุ หมายถึงวันนี้
176:      * @param string     $format รูปแบบของวันที่ที่ต้องการ (ถ้าไม่ระบุจะใช้รูปแบบที่มาจากระบบภาษา DATE_FORMAT)
177:      *
178:      * @return string
179:      */
180:     public static function format($time = 0, $format = null)
181:     {
182:         if ($time === 0) {
183:             $time = time();
184:         } elseif (is_string($time)) {
185:             if (preg_match('/^[0-9]+$/', $time)) {
186:                 $time = (int) $time;
187:             } else {
188:                 $time = strtotime($time);
189:             }
190:         } elseif (!is_int($time)) {
191:             return '';
192:         }
193:         // create class
194:         if (!isset(self::$lang)) {
195:             new static();
196:         }
197:         $format = empty($format) ? 'DATE_FORMAT' : $format;
198:         $format = Language::get($format);
199:         if (preg_match_all('/(.)/u', $format, $match)) {
200:             $ret = '';
201:             foreach ($match[0] as $item) {
202:                 switch ($item) {
203:                     case ' ':
204:                     case ':':
205:                     case '/':
206:                     case '-':
207:                     case '.':
208:                     case ',':
209:                         $ret .= $item;
210:                         break;
211:                     case 'l':
212:                         $ret .= self::$lang['DATE_SHORT'][date('w', $time)];
213:                         break;
214:                     case 'L':
215:                         $ret .= self::$lang['DATE_LONG'][date('w', $time)];
216:                         break;
217:                     case 'M':
218:                         $ret .= self::$lang['MONTH_SHORT'][date('n', $time)];
219:                         break;
220:                     case 'F':
221:                         $ret .= self::$lang['MONTH_LONG'][date('n', $time)];
222:                         break;
223:                     case 'Y':
224:                         $ret .= (int) date('Y', $time) + (int) self::$lang['YEAR_OFFSET'];
225:                         break;
226:                     default:
227:                         $ret .= trim($item) == '' ? ' ' : date($item, $time);
228:                         break;
229:                 }
230:             }
231:         } else {
232:             $ret = date($format, $time);
233:         }
234:         return $ret;
235:     }
236: 
237:     /**
238:      * แปลงตัวเลขเป็นชื่อเดือนตามภาษาที่ใช้งานอยู่
239:      * คืนค่า 1 มกราคม...12 ธันวาคม
240:      *
241:      * @assert (1) [==] 'ม.ค.'
242:      * @assert (1, false) [==] 'มกราคม'
243:      *
244:      * @param int  $month       1-12
245:      * @param bool $short_month true (default) ชื่อเดือนแบบสั้น เช่น มค., false ชื่อเดือนแบบเต็ม เช่น มกราคม
246:      *
247:      * @return string
248:      */
249:     public static function monthName($month, $short_month = true)
250:     {
251:         // create class
252:         if (!isset(self::$lang)) {
253:             new static();
254:         }
255:         $var = $short_month ? self::$lang['MONTH_SHORT'] : self::$lang['MONTH_LONG'];
256:         return isset($var[$month]) ? $var[$month] : '';
257:     }
258: 
259:     /**
260:      * แยกวันที่ออกเป็น array
261:      * คืนค่า array(y, m, d, h, i, s) หรือ array(y, m, d) หากเป้นวันที่อย่างเดียว หรือ false หากไม่ใช่วันที่
262:      *
263:      * @param string $date
264:      *
265:      * @return array|bool
266:      */
267:     public static function parse($date)
268:     {
269:         if (preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $date, $match)) {
270:             if (isset($match[4])) {
271:                 return array('y' => $match[1], 'm' => $match[2], 'd' => $match[3], 'h' => $match[5], 'i' => $match[6], 's' => $match[7]);
272:             } else {
273:                 return array('y' => $match[1], 'm' => $match[2], 'd' => $match[3]);
274:             }
275:         }
276:         return false;
277:     }
278: }
279: 
Kotchasan API documentation generated by ApiGen