TimeStamp.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\model\concern;
  13. use DateTime;
  14. /**
  15. * 自动时间戳
  16. */
  17. trait TimeStamp
  18. {
  19. /**
  20. * 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
  21. * @var bool|string
  22. */
  23. protected $autoWriteTimestamp;
  24. /**
  25. * 创建时间字段 false表示关闭
  26. * @var false|string
  27. */
  28. protected $createTime = 'create_time';
  29. /**
  30. * 更新时间字段 false表示关闭
  31. * @var false|string
  32. */
  33. protected $updateTime = 'update_time';
  34. /**
  35. * 时间字段显示格式
  36. * @var string
  37. */
  38. protected $dateFormat;
  39. /**
  40. * 是否需要自动写入时间字段
  41. * @access public
  42. * @param bool|string $auto
  43. * @return $this
  44. */
  45. public function isAutoWriteTimestamp($auto)
  46. {
  47. $this->autoWriteTimestamp = $this->checkTimeFieldType($auto);
  48. return $this;
  49. }
  50. /**
  51. * 检测时间字段的实际类型
  52. * @access public
  53. * @param bool|string $type
  54. * @return mixed
  55. */
  56. protected function checkTimeFieldType($type)
  57. {
  58. if (true === $type) {
  59. if (isset($this->type[$this->createTime])) {
  60. $type = $this->type[$this->createTime];
  61. } elseif (isset($this->schema[$this->createTime]) && in_array($this->schema[$this->createTime], ['datetime', 'date', 'timestamp', 'int'])) {
  62. $type = $this->schema[$this->createTime];
  63. } else {
  64. $type = $this->getFieldType($this->createTime);
  65. }
  66. }
  67. return $type;
  68. }
  69. /**
  70. * 获取自动写入时间字段
  71. * @access public
  72. * @return bool|string
  73. */
  74. public function getAutoWriteTimestamp()
  75. {
  76. return $this->autoWriteTimestamp;
  77. }
  78. /**
  79. * 设置时间字段格式化
  80. * @access public
  81. * @param string|false $format
  82. * @return $this
  83. */
  84. public function setDateFormat($format)
  85. {
  86. $this->dateFormat = $format;
  87. return $this;
  88. }
  89. /**
  90. * 获取自动写入时间字段
  91. * @access public
  92. * @return string|false
  93. */
  94. public function getDateFormat()
  95. {
  96. return $this->dateFormat;
  97. }
  98. /**
  99. * 自动写入时间戳
  100. * @access protected
  101. * @return mixed
  102. */
  103. protected function autoWriteTimestamp()
  104. {
  105. // 检测时间字段类型
  106. $type = $this->checkTimeFieldType($this->autoWriteTimestamp);
  107. return is_string($type) ? $this->getTimeTypeValue($type) : time();
  108. }
  109. /**
  110. * 获取指定类型的时间字段值
  111. * @access protected
  112. * @param string $type 时间字段类型
  113. * @return mixed
  114. */
  115. protected function getTimeTypeValue(string $type)
  116. {
  117. $value = time();
  118. switch ($type) {
  119. case 'datetime':
  120. case 'date':
  121. case 'timestamp':
  122. $value = $this->formatDateTime('Y-m-d H:i:s.u');
  123. break;
  124. default:
  125. if (false !== strpos($type, '\\')) {
  126. // 对象数据写入
  127. $obj = new $type();
  128. if (method_exists($obj, '__toString')) {
  129. // 对象数据写入
  130. $value = $obj->__toString();
  131. }
  132. }
  133. }
  134. return $value;
  135. }
  136. /**
  137. * 时间日期字段格式化处理
  138. * @access protected
  139. * @param mixed $format 日期格式
  140. * @param mixed $time 时间日期表达式
  141. * @param bool $timestamp 时间表达式是否为时间戳
  142. * @return mixed
  143. */
  144. protected function formatDateTime($format, $time = 'now', bool $timestamp = false)
  145. {
  146. if (empty($time)) {
  147. return;
  148. }
  149. if (false === $format) {
  150. return $time;
  151. } elseif (false !== strpos($format, '\\')) {
  152. return new $format($time);
  153. }
  154. if ($time instanceof DateTime) {
  155. $dateTime = $time;
  156. } elseif ($timestamp) {
  157. $dateTime = new DateTime();
  158. $dateTime->setTimestamp((int) $time);
  159. } else {
  160. $dateTime = new DateTime($time);
  161. }
  162. return $dateTime->format($format);
  163. }
  164. /**
  165. * 获取时间字段值
  166. * @access protected
  167. * @param mixed $value
  168. * @return mixed
  169. */
  170. protected function getTimestampValue($value)
  171. {
  172. $type = $this->checkTimeFieldType($this->autoWriteTimestamp);
  173. if (is_string($type) && in_array(strtolower($type), [
  174. 'datetime', 'date', 'timestamp',
  175. ])) {
  176. $value = $this->formatDateTime($this->dateFormat, $value);
  177. } else {
  178. $value = $this->formatDateTime($this->dateFormat, $value, true);
  179. }
  180. return $value;
  181. }
  182. }