2018 九月

版权声明:未经允许不得转载。 https://blog.csdn.net/qq_35958788/article/details/82793609

1.写一个函数,输入为生日,时间点,求周岁,错误返回false

<?php

function getAge($bornDate, $givenTime = 0) {
    try {
        if (!is_numeric($bornDate)) {
            $bornDate = strtotime($bornDate);
        }
        if ($givenTime === 0) {
            $givenTime = time();
        } elseif (!is_numeric($givenTime)) {
            $givenTime = strtotime($givenTime);
        }
    } catch (Exception $e) {
        return false;
    }
    if ($bornDate >= $givenTime) {
        return false;
    }
    $age = date('Y', $givenTime) - date('Y', $bornDate);
    echo date('Y', $bornDate);
    if (date('m-d', $givenTime) < date('m-d', $bornDate)) {
        $age -= 1;
    }
    return $age;
}

2.一个对文件的增删改查类

<?php
class FileHandle
{
    private $long = 1024;

    public function create()
    {

    }

    public function del($filename)
    {
        unlink($filename);
    }

    public function append($file_path, $string)
    {
        $fp = $this->open($file_path, 'a');
        fwrite($fp, $string);
        $this->close($fp);
    }

    public function view($file_path)
    {
        $fp = $this->open($file_path);
        $file_info = fstat($fp);
        $content = '';

        while (feof($fp)) {
            $content .= fread($fp, $this->long);
        }

        $this->close($fp);
        return ['file_info' => $file_info, 'content' => $content];
    }

    private function open($file_path, $mode = 'r')
    {
        $fp = fopen($file_path, $mode);
        return $fp;
    }

    private function close($fp)
    {
        fclose($fp);
    }
}

3.对一个数组按元素的出现次数排序,出现次数的多的元素最前面,想同次数的元素先出现的在前,如 [a, b, c, d, c, c, d, a, d], 得到 [c, d, a, b]。

function repeatLetterSort($data) {
    $resScore = [];
    foreach ($data as $v) {
        if (isset($resScore[$v])) {
            $resScore[$v]++;
        } else {
            $resScore[$v] = 1;
        }
    }
    arsort($resScore);
    return array_keys($resScore);
}

猜你喜欢

转载自blog.csdn.net/qq_35958788/article/details/82793609