1
<?php
2

3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Carbon\Traits;
12

13
/**
14
 * Trait Timestamp.
15
 */
16
trait Timestamp
17
{
18
    /**
19
     * Create a Carbon instance from a timestamp and set the timezone (use default one if not specified).
20
     *
21
     * Timestamp input can be given as int, float or a string containing one or more numbers.
22
     *
23
     * @param float|int|string          $timestamp
24
     * @param \DateTimeZone|string|null $tz
25
     *
26
     * @return static
27
     */
28 1
    public static function createFromTimestamp($timestamp, $tz = null)
29
    {
30 1
        return static::createFromTimestampUTC($timestamp)->setTimezone($tz);
31
    }
32

33
    /**
34
     * Create a Carbon instance from an timestamp keeping the timezone to UTC.
35
     *
36
     * Timestamp input can be given as int, float or a string containing one or more numbers.
37
     *
38
     * @param float|int|string $timestamp
39
     *
40
     * @return static
41
     */
42 1
    public static function createFromTimestampUTC($timestamp)
43
    {
44 1
        [$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp);
45 1
        $delta = floor($decimal / static::MICROSECONDS_PER_SECOND);
46 1
        $integer += $delta;
47 1
        $decimal -= $delta * static::MICROSECONDS_PER_SECOND;
48

49 1
        return static::rawCreateFromFormat('U u', "$integer $decimal");
50
    }
51

52
    /**
53
     * Create a Carbon instance from a timestamp in milliseconds.
54
     *
55
     * Timestamp input can be given as int, float or a string containing one or more numbers.
56
     *
57
     * @param float|int|string $timestamp
58
     *
59
     * @return static
60
     */
61 1
    public static function createFromTimestampMsUTC($timestamp)
62
    {
63 1
        [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3);
64 1
        $sign = $milliseconds < 0 || $milliseconds === 0.0 && $microseconds < 0 ? -1 : 1;
65 1
        $milliseconds = abs($milliseconds);
66 1
        $microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND);
67 1
        $seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND);
68 1
        $delta = floor($microseconds / static::MICROSECONDS_PER_SECOND);
69 1
        $seconds += $delta;
70 1
        $microseconds -= $delta * static::MICROSECONDS_PER_SECOND;
71 1
        $microseconds = str_pad($microseconds, 6, '0', STR_PAD_LEFT);
72

73 1
        return static::rawCreateFromFormat('U u', "$seconds $microseconds");
74
    }
75

76
    /**
77
     * Create a Carbon instance from a timestamp in milliseconds.
78
     *
79
     * Timestamp input can be given as int, float or a string containing one or more numbers.
80
     *
81
     * @param float|int|string          $timestamp
82
     * @param \DateTimeZone|string|null $tz
83
     *
84
     * @return static
85
     */
86 1
    public static function createFromTimestampMs($timestamp, $tz = null)
87
    {
88 1
        return static::createFromTimestampMsUTC($timestamp)
89 1
            ->setTimezone($tz);
90
    }
91

92
    /**
93
     * Set the instance's timestamp.
94
     *
95
     * Timestamp input can be given as int, float or a string containing one or more numbers.
96
     *
97
     * @param float|int|string $unixTimestamp
98
     *
99
     * @return static
100
     */
101 1
    public function timestamp($unixTimestamp)
102
    {
103 1
        return $this->setTimestamp($unixTimestamp);
104
    }
105

106
    /**
107
     * Returns a timestamp rounded with the given precision (6 by default).
108
     *
109
     * @example getPreciseTimestamp()   1532087464437474 (microsecond maximum precision)
110
     * @example getPreciseTimestamp(6)  1532087464437474
111
     * @example getPreciseTimestamp(5)  153208746443747  (1/100000 second precision)
112
     * @example getPreciseTimestamp(4)  15320874644375   (1/10000 second precision)
113
     * @example getPreciseTimestamp(3)  1532087464437    (millisecond precision)
114
     * @example getPreciseTimestamp(2)  153208746444     (1/100 second precision)
115
     * @example getPreciseTimestamp(1)  15320874644      (1/10 second precision)
116
     * @example getPreciseTimestamp(0)  1532087464       (second precision)
117
     * @example getPreciseTimestamp(-1) 153208746        (10 second precision)
118
     * @example getPreciseTimestamp(-2) 15320875         (100 second precision)
119
     *
120
     * @param int $precision
121
     *
122
     * @return float
123
     */
124 1
    public function getPreciseTimestamp($precision = 6)
125
    {
126 1
        return round($this->rawFormat('Uu') / pow(10, 6 - $precision));
127
    }
128

129
    /**
130
     * Returns the milliseconds timestamps used amongst other by Date javascript objects.
131
     *
132
     * @return float
133
     */
134 1
    public function valueOf()
135
    {
136 1
        return $this->getPreciseTimestamp(3);
137
    }
138

139
    /**
140
     * @alias getTimestamp
141
     *
142
     * Returns the UNIX timestamp for the current date.
143
     *
144
     * @return int
145
     */
146 1
    public function unix()
147
    {
148 1
        return $this->getTimestamp();
149
    }
150

151
    /**
152
     * Return an array with integer part digits and decimals digits split from one or more positive numbers
153
     * (such as timestamps) as string with the given number of decimals (6 by default).
154
     *
155
     * By splitting integer and decimal, this method obtain a better precision than
156
     * number_format when the input is a string.
157
     *
158
     * @param float|int|string $numbers  one or more numbers
159
     * @param int              $decimals number of decimals precision (6 by default)
160
     *
161
     * @return array 0-index is integer part, 1-index is decimal part digits
162
     */
163 1
    private static function getIntegerAndDecimalParts($numbers, $decimals = 6)
164
    {
165 1
        if (\is_int($numbers) || \is_float($numbers)) {
166 1
            $numbers = number_format($numbers, $decimals, '.', '');
167
        }
168

169 1
        $sign = substr($numbers, 0, 1) === '-' ? -1 : 1;
170 1
        $integer = 0;
171 1
        $decimal = 0;
172

173 1
        foreach (preg_split('`[^0-9.]+`', $numbers) as $chunk) {
174 1
            [$integerPart, $decimalPart] = explode('.', "$chunk.");
175

176 1
            $integer += \intval($integerPart);
177 1
            $decimal += \floatval("0.$decimalPart");
178
        }
179

180 1
        $overflow = floor($decimal);
181 1
        $integer += $overflow;
182 1
        $decimal -= $overflow;
183

184 1
        return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))];
185
    }
186
}

Read our documentation on viewing source code .

Loading