1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace think\model\concern;
- use DateTime;
- trait TimeStamp
- {
-
- protected $autoWriteTimestamp;
-
- protected $createTime = 'create_time';
-
- protected $updateTime = 'update_time';
-
- protected $dateFormat;
-
- protected function formatDateTime($format, $time = 'now', $timestamp = false)
- {
- if (empty($time)) {
- return;
- }
- if (false === $format) {
- return $time;
- } elseif (false !== strpos($format, '\\')) {
- return new $format($time);
- }
- if ($timestamp) {
- $dateTime = new DateTime();
- $dateTime->setTimestamp($time);
- } else {
- $dateTime = new DateTime($time);
- }
- return $dateTime->format($format);
- }
-
- protected function checkTimeStampWrite()
- {
-
- if ($this->autoWriteTimestamp) {
- if ($this->createTime && !isset($this->data[$this->createTime])) {
- $this->data[$this->createTime] = $this->autoWriteTimestamp($this->createTime);
- }
- if ($this->updateTime && !isset($this->data[$this->updateTime])) {
- $this->data[$this->updateTime] = $this->autoWriteTimestamp($this->updateTime);
- }
- }
- }
- }
|