course/app/functions.php

131 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* Here is your custom functions.
*/
if (!function_exists('generate_order_no')) {
/**
* 生成 前缀+24位数字的订单号
* @param string $prefix 前缀
* @return string
*/
function generate_order_no($prefix = '')
{
$randLen = 6;
$id = base_convert(substr(uniqid(), 0 - $randLen), 16, 10);
if (strlen($id) > 10) {
$id = substr($id, -10);
} elseif (strlen($id) < 10) {
$rLen = 10 - strlen($id);
$id = $id . rand(pow(10, $rLen - 1), pow(10, $rLen) - 1);
}
$dateTimeStr = date('YmdHis');
return $prefix . $dateTimeStr . $id;
}
}
/**
* 记录日志,默认存放路径为:{$config_path}/logs/{$dir_name}/ddddmm/d.log
* @dir_name logs/下的目录路径
* @data 日志数据
*/
if (!function_exists('raw_log')) {
function raw_log(string $dir_name, $data)
{
$log_path = runtime_path() . "/logs/{$dir_name}/" . date('Ym');
if (!is_dir($log_path)) {
mkdir($log_path, 0777, true);
}
$log_path .= '/' . date('d') . '.log';
$log = new Monolog\Logger($dir_name);
$log->pushHandler(new \Monolog\Handler\StreamHandler($log_path, \Monolog\Logger::INFO));
$log->info('', $data);
}
}
/**
* @desc 生成随机字符串
* @param $len
* @return string
* @throws \Random\RandomException
*/
if (!function_exists('random_str')) {
function random_str($len = 32)
{
return bin2hex(random_bytes(round($len / 2)));
}
}
2024-08-10 18:15:21 +08:00
if (!function_exists('get_dates_in_month')) {
/**
* @desc 获取月份所有天
* @param $year
* @param $month
* @return array
*/
function get_dates_in_month($year, $month) {
$dates = [];
$firstDayOfMonth = new DateTime("$year-$month-01");
$lastDayOfMonth = clone $firstDayOfMonth;
$lastDayOfMonth->modify('last day of this month');
for ($currentDate = $firstDayOfMonth; $currentDate <= $lastDayOfMonth; $currentDate->modify('+1 day')) {
$dates[] = $currentDate->format('Y-m-d');
}
return $dates;
}
}
2024-10-20 22:50:43 +08:00
if(!function_exists('is_time_cross')){
/**
* PHP计算多个时间段是否有交集边界重叠不算
* @param ...$timePeriods
* @return bool|mixed
*/
function is_time_cross(array $timePeriods) {
$timePeriods = array_values($timePeriods);
// 将时间字符串转换为时间戳
$timePeriods = array_map(function ($period) {
extract($period);
return [
'begin' => strtotime($beginTime),
'end' => strtotime($endTime)
];
}, $timePeriods);
// 递归函数来检查时间段是否有交集
$checkCross = function ($index, $timePeriods) use (&$checkCross) {
if ($index === count($timePeriods) - 1) {
return true; // 最后一个时间段,没有交集
}
for ($i = $index + 1; $i < count($timePeriods); $i++) {
if ($timePeriods[$index]['end'] > $timePeriods[$i]['begin'] && $timePeriods[$i]['end'] > $timePeriods[$index]['begin']) {
return false; // 有交集
}
}
return $checkCross($index + 1, $timePeriods); // 递归检查下一个时间段
};
return $checkCross(0, $timePeriods); // 从第一个时间段开始检查
}
}
2025-05-07 16:02:02 +08:00
//写一个检测字符串是否包含中文的函数
if(!function_exists('check_chinese_chars')){
function check_chinese_chars($str){
return preg_match('/[\x{4e00}-\x{9fa5}]/u', $str);
}
}