| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- /**
- * Here is your custom functions.
- */
- // 验证日期格式是否有问题
- function isValidDate($date, $format = 'Y-m-d'): bool
- {
- $d = DateTime::createFromFormat($format, $date);
- return $d && $d->format($format) === $date;
- }
- /**
- * 获取两个日期之间的所有月份(格式:202508)
- *
- * @param string $startDate 开始日期(格式:Y-m-d)
- * @param string $endDate 结束日期(格式:Y-m-d)
- * @return array 月份数组,格式为 ['202508', '202509', ...]
- */
- function getMonthsBetweenDates($startDate, $endDate) {
- $months = [];
- // 转换为 DateTime 对象
- $start = new DateTime($startDate);
- $end = new DateTime($endDate);
- // 确保开始日期不大于结束日期
- if ($start > $end) {
- return $months;
- }
- // 设置开始日期为当月的第一天
- $start->modify('first day of this month');
- // 循环直到超过结束日期
- while ($start <= $end) {
- // 添加当前月份到数组(格式:202508)
- $months[] = $start->format('Ym');
- // 增加一个月
- $start->add(new DateInterval('P1M'));
- }
- return $months;
- }
- // 获取IP地址
- function getIpLocation($ip): string
- {
- $ip2region = new \Ip2Region();
- try {
- $region = $ip2region->memorySearch($ip);
- } catch (\Exception $e) {
- return '未知';
- }
- list($country, $number, $province, $city, $network) = explode('|', $region['region']);
- if ($network === '内网IP') {
- return $network;
- }
- if ($country == '中国') {
- return $province.'-'.$city.':'.$network;
- } else if ($country == '0') {
- return '未知';
- } else {
- return $country;
- }
- }
|