-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoment.php
120 lines (112 loc) · 2.73 KB
/
Moment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Yonna\Foundation;
class Moment
{
/**
* 获取一个时间戳在该周内的星期一
* @param $timestamp
* @return bool|string
*/
public static function getMonday($timestamp)
{
$timestamp2 = strtotime(date("Y-m-d", $timestamp) . " last monday");
if (($timestamp - $timestamp2) / 86400 == 7) {
return date("Y-m-d", $timestamp);
}
return date("Y-m-d", $timestamp2);
}
/**
* 获取一个时间戳在该季度内的首月
* @param $timestamp
* @return string
*/
public static function getQuarterFirstMonth($timestamp): string
{
$m = intval(date('m', $timestamp));
switch ($m) {
case 1:
case 2:
case 3:
$m = '01';
break;
case 4:
case 5:
case 6:
$m = '04';
break;
case 7:
case 8:
case 9:
$m = '07';
break;
case 10:
case 11:
case 12:
$m = '10';
break;
}
return $m;
}
/**
* 获取一个时间戳在该季度内的最后一个月
* @param $timestamp
* @return bool|string
*/
public static function getQuarterLastMonth($timestamp)
{
$m = date('m', $timestamp);
switch ($m) {
case 1:
case 2:
case 3:
$m = '03';
break;
case 4:
case 5:
case 6:
$m = '06';
break;
case 7:
case 8:
case 9:
$m = '09';
break;
case 10:
case 11:
case 12:
$m = 12;
break;
}
return $m;
}
/**
* @param $str
* @param bool $isFloat
* @param int $deep 深度 毫秒 000 微秒 000000
* @return string | array
*/
public static function strToMicrotime($str, $isFloat = false, $deep = 6)
{
$micros = explode('.', $str);
$micro = str_repeat('0', $deep);
if (is_array($micros) && count($micros) === 2) {
$micro = $micros[1];
}
if ($isFloat) {
return floatval(strtotime($str) . '.' . $micro);
} else {
$micros[1] = $micro;
return $micros;
}
}
/**
* @param $format
* @param $val
* @return string
*/
public static function datetimeMicro($format, $val)
{
$micros = static::strToMicrotime($val);
return date($format, strtotime($micros[0])) . '.' . $micros[1];
}
}