编辑代码

<?php
// 获取今天开始和结束的时间戳
$today_start = strtotime(date('Y-m-d'));
$today_end = $today_start + 86399;

// 获取本周开始和结束的时间戳
$week_start = strtotime('this week Monday', time());
$week_end = $week_start + 6 * 86400;

// 获取本月开始和结束的时间戳
$month_start = strtotime(date('Y-m-01'));
$month_end = strtotime(date('Y-m-t'));

echo "今天开始时间戳: " . $today_start . "
".date('Y-m-d H:i:s',$today_start);
echo "今天结束时间戳: " . $today_end . "
";
echo "本周开始时间戳: " . $week_start . "
";
echo "本周结束时间戳: " . $week_end . "
";
echo "本月开始时间戳: " . $month_start . "
";
echo "本月结束时间戳: " . $month_end . "
";
function dateDifferenceInMonths($startDate, $endDate) {
    // 校验参数的合法性
    if (!is_string($startDate) || !is_string($endDate)) {
        throw new InvalidArgumentException("Both startDate and endDate must be strings.");
    }

    $start = DateTime::createFromFormat('Y-m-d', $startDate);
    $end = DateTime::createFromFormat('Y-m-d', $endDate);

    if ($start === false || $end === false) {
        throw new InvalidArgumentException("Invalid date format. Use 'Y-m-d'.");
    }

    if ($start > $end) {
        throw new InvalidArgumentException("开始日期必须小于或等于结束日期。");
    }

    // 计算两个日期之间的月份差
    $interval = $start->diff($end);
    $months = $interval->y * 12 + $interval->m;

    return $months;
}

// 示例用法

$total_loan = 30000; // 总借款额
$annual_interest_rate = 15 / 100; // 年利率
$monthly_interest_rate = $annual_interest_rate / 12; // 月利率
$number_of_payments = 24; // 分期期数

// 初始化数组
$loan_schedule = [];

for ($i = 1; $i <= $number_of_payments; $i++) {
    $benjin = $total_loan / $number_of_payments; // 每期本金
    $remaining_loan = $total_loan - ($i - 1) * $benjin; // 剩余未还本金
    $lixi = $remaining_loan * $monthly_interest_rate; // 每期利息
    $price = $benjin + $lixi; // 每期应还总额

    // 将结果存入数组
    $loan_schedule[] = [
        'benjin' => round($benjin, 2),
        'lixi' => round($lixi, 2),
        'price' => round($price, 2)
    ];
}

// 输出每期的还款信息
// foreach ($loan_schedule as $key => $payment) {
//     echo "第".($key + 1)."期:本金 {$payment['benjin']} 元,利息 {$payment['lixi']} 元,总额 {$payment['price']} 元\r\n";
// }


// $principal = 30000; // 本金
// $interestRate = 0.15; // 利率
// $term = 24; // 借款期限(月)
// $loanDate = '2024-08-01'; // 借款时间

// $monthlyInterestRate = $interestRate / 12;
// $monthlyPayment = $principal * $monthlyInterestRate * pow(1 + $monthlyInterestRate, $term) / (pow(1 + $monthlyInterestRate, $term) - 1);

// $repaymentDetails = [];

// for ($i = 1; $i <= $term; $i++) {
//     $interestPayment = $principal * $monthlyInterestRate;
//     $principalPayment = $monthlyPayment - $interestPayment;

//     $repaymentDetails[] = [
//         'month' => $i,
//         'time' => date('Y-m-d', strtotime($loanDate . " + $i month")),
//         'price' => $monthlyPayment,
//         'benjin' => $principalPayment,
//         '利息' => $interestPayment
//     ];

//     $principal -= $principalPayment;
// }

// echo "<pre>";
// print_r($repaymentDetails);
// echo "</pre>";
// function calculateRepaymentDetails($principal, $interestRate, $term, $loanDate) {
//     $monthlyInterestRate = $interestRate / 12;
    
//     $repaymentDetails = [];
//     $totalInterest = 0;

//     for ($i = 1; $i <= $term; $i++) {
//         $interestPayment = $principal * $monthlyInterestRate;
//         $totalInterest += $interestPayment;
        
//         if ($i == $term) {
//             $totalPayment = $principal + $totalInterest;
//         } else {
//             $totalPayment = $interestPayment;
//         }

//         $repaymentDetails[] = [
//             'month' => $i,
//             'time' => date('Y-m-d', strtotime($loanDate . " + $i month")),
//             'price' => $totalPayment,
//             'benjin' => $principal,
//             'lixi' => $interestPayment
//         ];
//     }

//     return $repaymentDetails;
// }

// $principal = 100000.00; // 本金
// $interestRate = 0.05; // 利率
// $term = 24; // 借款期限(月)
// $loanDate = '2024-08-02'; // 借款时间

// $repaymentDetails = calculateRepaymentDetails($principal, $interestRate, $term, $loanDate);

// echo "<pre>";
// print_r($repaymentDetails);
// echo "</pre>";

// function calculateRepayment($principal, $annualRate, $totalMonths, $startDate) {
//     $monthlyInterestRate = $annualRate / 12 / 100;
//     $repaymentArray = [];
//     for ($i = 1; $i <= $totalMonths; $i++) {
//         $interest = $principal * $monthlyInterestRate;
//         $repaymentAmount = $interest + $principal / $totalMonths;
//         $repaymentArray[] = [
//             'month' => $i,
//             'time' => date('Y-m-d', strtotime("+$i months", strtotime($startDate))),
//             'total' => round($repaymentAmount, 2),
//             'benjin' => round($principal / $totalMonths, 2),
//             'lixi' => round($interest, 2)
//         ];
//         $principal -= $principal / $totalMonths;
//     }
//     return $repaymentArray;
// }

// $principal = 100000.00;
// $annualRate = 5;
// $totalMonths = 24;
// $startDate = '2024-08-02';

// $result = calculateRepayment($principal, $annualRate, $totalMonths, $startDate);
$str = "{"qdName":"������������","source":"������������","name":"����","phone":"18811111113","price":50000,"house":1,"car":1,"insurance":0,"fund":1,"socital":0,"xyk":0,"wld":1,"qyns":1,"df":1,"business_license":1,"city":"�ɶ���","age":28,"loan_periods":36}";
var_dump(json_decode($str,true));

function calculateRepaymentSchedule($principal, $interestRate, $periods, $loanDate) {
    $repaymentSchedule = [];
    $monthlyInterestRate = $interestRate / 100 / 12;
    $monthlyPrincipal = $principal / $periods;

    for ($i = 1; $i <= $periods; $i++) {
        $interest = $principal * $monthlyInterestRate;
        $totalAmount = $monthlyPrincipal + $interest;
        $time = date('Y-m-d', strtotime("$loanDate + $i months"));

        $repaymentSchedule[] = [
            'month' => $i,
            'time' => $time,
            'totalAmount' => round($totalAmount, 2),
            'benjin' => round($monthlyPrincipal, 2),
            'lixi' => round($interest, 2)
        ];
    }

    return $repaymentSchedule;
}

$principal = 100000.00;
$interestRate = 5;
$periods = 24;
$loanDate = '2024-08-02';

// $result = calculateRepaymentSchedule($principal, $interestRate, $periods, $loanDate);
// print_r($result);


function generateActivationCode($totalLength) {
    // 确保总长度在能形成四段的基础上
    if (($totalLength - 3) % 4 != 0) {
        throw new Exception("Total length must be a multiple of 4 plus 3 to form 4 segments.");
    }

    // 计算每段的基础长度(不包括"-"$segmentLength = floor(($totalLength - 3) / 4);

    // 生成包含50%字母和50%数字的字符串
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $length = strlen($chars);
    $randomString = '';
    for ($i = 0; $i < $totalLength - 3; $i++) {
        $randomString .= $chars[rand(0, $length - 1)];
    }

    // 打乱字符串以确保随机性
    $randomString = str_shuffle($randomString);

    // 分配字母和数字各占50%可能比较复杂,这里简化处理
    // 实际上,由于$chars同时包含字母和数字,且随机选择,这已接近50%

    // 将字符串分为四段
    $segments = str_split($randomString, $segmentLength);

    // 添加"-"分隔符
    return implode('-', $segments);
}

// 示例:生成一个长度为15(实际字符长度为12,加上3个"-")的激活码
try {
    $activationCode = generateActivationCode(15);
    echo $activationCode;
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}