time.func.php 906 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2019-11-04 18:43:48
  7. */
  8. declare(strict_types=1);
  9. /**
  10. * 转换至mongoISODate格式.
  11. * @param mixed $date 任意有效的时间格式.
  12. * @param mixed $tz 时区
  13. * @return string|null
  14. */
  15. function toMongoIsoDate($date, $tz = null)
  16. {
  17. if (empty($tz)) {
  18. $tz = env('APP_TIMEZONE', null);
  19. }
  20. $millSecond = \Carbon\Carbon::parse($date)->timestamp * 1000;
  21. $isoDateObj = new MongoDB\BSON\UTCDateTime($millSecond);
  22. $isoDate = $isoDateObj->toDateTime()->setTimezone(new DateTimeZone($tz));
  23. return $isoDateObj;
  24. }
  25. /**
  26. * mongoIso时间转CarbonTime时间
  27. * @param \MongoDB\BSON\UTCDateTime $utcDate
  28. * @return \Carbon\Carbon
  29. */
  30. function mongoIsoDateToCarbonTime(\MongoDB\BSON\UTCDateTime $utcDate)
  31. {
  32. return \Carbon\Carbon::parse($utcDate->toDateTime()->setTimezone(new DateTimeZone(env('APP_TIMEZONE'))));
  33. }