Timestamp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Carbon\Traits;
  11. use DateTime;
  12. /**
  13. * Trait Timestamp.
  14. */
  15. trait Timestamp
  16. {
  17. /**
  18. * Create a Carbon instance from a timestamp.
  19. *
  20. * @param int $timestamp
  21. * @param \DateTimeZone|string|null $tz
  22. *
  23. * @return static
  24. */
  25. public static function createFromTimestamp($timestamp, $tz = null)
  26. {
  27. $date = new DateTime('@'.((int) $timestamp));
  28. $tz = static::safeCreateDateTimeZone($tz);
  29. if ($tz) {
  30. $date->setTimezone($tz);
  31. }
  32. return (new static($date->format(DateTime::ATOM)))->tz($tz);
  33. }
  34. /**
  35. * Create a Carbon instance from a timestamp in milliseconds.
  36. *
  37. * @param float $timestamp
  38. * @param \DateTimeZone|string|null $tz
  39. *
  40. * @return static
  41. */
  42. public static function createFromTimestampMs($timestamp, $tz = null)
  43. {
  44. return static::rawCreateFromFormat('U.u', sprintf('%F', $timestamp / 1000))
  45. ->setTimezone($tz);
  46. }
  47. /**
  48. * Create a Carbon instance from an UTC timestamp.
  49. *
  50. * @param int $timestamp
  51. *
  52. * @return static
  53. */
  54. public static function createFromTimestampUTC($timestamp)
  55. {
  56. return new static('@'.$timestamp);
  57. }
  58. /**
  59. * Set the instance's timestamp.
  60. *
  61. * @param int $value
  62. *
  63. * @return static
  64. */
  65. public function timestamp($value)
  66. {
  67. return $this->setTimestamp((int) $value);
  68. }
  69. /**
  70. * Returns a timestamp rounded with the given precision (6 by default).
  71. *
  72. * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision)
  73. * @example getPreciseTimestamp(6) 1532087464437474
  74. * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision)
  75. * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision)
  76. * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision)
  77. * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision)
  78. * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision)
  79. * @example getPreciseTimestamp(0) 1532087464 (second precision)
  80. * @example getPreciseTimestamp(-1) 153208746 (10 second precision)
  81. * @example getPreciseTimestamp(-2) 15320875 (100 second precision)
  82. *
  83. * @param int $precision
  84. *
  85. * @return float
  86. */
  87. public function getPreciseTimestamp($precision = 6)
  88. {
  89. return round($this->rawFormat('Uu') / pow(10, 6 - $precision));
  90. }
  91. /**
  92. * Returns the milliseconds timestamps used amongst other by Date javascript objects.
  93. *
  94. * @return float
  95. */
  96. public function valueOf()
  97. {
  98. return $this->getPreciseTimestamp(3);
  99. }
  100. /**
  101. * @alias getTimestamp
  102. *
  103. * Returns the UNIX timestamp for the current date.
  104. *
  105. * @return int
  106. */
  107. public function unix()
  108. {
  109. return $this->getTimestamp();
  110. }
  111. }