<?php

namespace PhpLife\Frame\Helper;

/**
 * 文件操作
 */
class Date extends \PhpLife\Frame\Helper
{

    /**
     * 转换为友好时间
     * @param int|string $timestamp
     * @return string
     */
    public static function convert($timestamp)
    {
        if (!is_numeric($timestamp)) {
            $timestamp = strtotime($timestamp);
        }
        $now = time();
        $interval = abs($now - $timestamp);
        $today = strtotime(date("Ymd"));
        $tomorrow = strtotime(date("Ymd", $now + 86400));
        $thisYear = strtotime(date("Y-01-01 00:00"));
        if ($interval < 60) {
            $string = "1分钟前";
        } else if ($interval < 3600) {
            $string = floor($interval / 60) . '分钟前';
        } else if ($today < $timestamp AND $timestamp < $tomorrow) {
            $string = '今天' . date('H:i', $timestamp);
        } else if ($thisYear < $timestamp) {
            $string = date('m-d H:i', $timestamp);
        } else {
            $string = date('y-m-d H:i', $timestamp);
        }
        return $string;
    }

    /**
     * 获取相对时间
     * @param $timestamp
     * @param $customString ;
     * @return string
     */
    public static function related($timestamp, $customString = '')
    {
        if (!is_numeric($timestamp)) {
            $timestamp = strtotime($timestamp);
        }
        $interval = abs(time() - $timestamp);
        if ($interval == 0) {
            $interval = 1;
        }
        if ($interval < 3600) {
            $string = ceil($interval / 60) . '分钟前';
        } elseif ($interval < 86400) {
            $string = ceil($interval / 3600) . '小时前';
        } else {
            $string = ceil($interval / 86400) . '天前';
        }
        $string = $string . $customString;
        return $string;
    }


}