本文最后更新于 2024-05-09,文章内容可能已经过时,请注意发布时间。

运行方式

php 文件名.php 年 月 日

年月日用空格隔开即可

代码

<?php
function get_zodiac_sign($day, $month) {
    $zodiac_signs = ['水瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手', '摩羯'];
    $zodiac_dates = [20, 19, 21, 20, 21, 22, 23, 23, 23, 23, 22, 21];

    return $zodiac_signs[array_search($day, $zodiac_dates) + 12 * floor(($month - 1) / 2) % 12];
}

function get_chinese_zodiac($year) {
    $chinese_zodiac = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];

    return $chinese_zodiac[($year - 4) % 12];
}

// 获取命令行参数
$year = $argv[1];
$month = $argv[2];
$day = $argv[3];

// 检查参数是否有效
if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)) {
    echo "请输入有效的年、月、日参数。\n";
    exit;
}

$birth_date = "$year-$month-$day";
$birth_timestamp = strtotime($birth_date);
$day = date('d', $birth_timestamp);
$month = date('m', $birth_timestamp);
$year = date('Y', $birth_timestamp);

$zodiac_sign = get_zodiac_sign($day, $month);
$chinese_zodiac = get_chinese_zodiac($year);

echo "出生日期:$birth_date\n";
echo "星座:$zodiac_sign\n";
echo "生肖:$chinese_zodiac\n";

升级版本

不需要每次都输入 php 文件名 年 月 日 只需要输入

php 文件名

然后输入 年 月 日 即可,如下图

升级版本代码

<?php
function get_zodiac_sign($day, $month) {
    $zodiac_signs = ['水瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手', '摩羯'];
    $zodiac_dates = [20, 19, 21, 20, 21, 22, 23, 23, 23, 23, 22, 21];

    return $zodiac_signs[array_search($day, $zodiac_dates) + 12 * floor(($month - 1) / 2) % 12];
}

function get_chinese_zodiac($year) {
    $chinese_zodiac = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];

    return $chinese_zodiac[($year - 4) % 12];
}

function draw_progress_bar($percentage) {
    $width = 50;
    $progress = floor($width * $percentage / 100);
    $remaining = $width - $progress;

    $bar = "\033[32m" . str_repeat("█", $progress) . "\033[37m" . str_repeat("░", $remaining) . "\033[0m";
    echo "进度:|{$bar}| {$percentage}%\r";
}

$total_steps = 100;
$current_step = 0;

while (true) {
    echo "请输入年、月、日(用空格分隔):";
    $input = trim(fgets(STDIN));
    $input_parts = explode(' ', $input);

    if (count($input_parts) !== 3) {
        echo "请输入有效的年、月、日参数。\n";
        continue;
    }

    list($year, $month, $day) = $input_parts;

    if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)) {
        echo "请输入有效的年、月、日参数。\n";
        continue;
    }

    $birth_date = "$year-$month-$day";
    $birth_timestamp = strtotime($birth_date);
    $day = date('d', $birth_timestamp);
    $month = date('m', $birth_timestamp);
    $year = date('Y', $birth_timestamp);

    $zodiac_sign = get_zodiac_sign($day, $month);
    $chinese_zodiac = get_chinese_zodiac($year);

    // 模拟进度条
    for ($i = 1; $i <= $total_steps; $i++) {
        $percentage = ($i / $total_steps) * 100;
        draw_progress_bar($percentage);
        usleep(5000/*0*/); // 暂停0.1秒
    }
    echo "\n";

    echo "出生日期:$birth_date\n";
    echo "星座:$zodiac_sign\n";
    echo "生肖:$chinese_zodiac\n";
}