Date.class.php 4.21 KB
<?php
/**
 * 时间
 * 
 * @name Date
 * @version 
 * @package QLib.Utils.Date
 * @author peter.zyliu liuziyang@gmail.com
 * @since 1.0
 */

class Util_Utils_Date {
	private static $DayMap = array(
		'前天', 
		'昨天', 
		'今天', 
		'明天', 
		'后天'
	);
	public static $WeekMap = array(
		'周日', 
		'周一', 
		'周二', 
		'周三', 
		'周四', 
		'周五', 
		'周六'
	);
	/**
	 * 活动显示的时间格式
	 *
	 * @param integer $timestamp
	 * @return string
	 */
	public static function activityDisplayDate($timestamp, $needSecond = false) {
		$format = date('Y') == date('Y', $timestamp) ? ($needSecond ? 'n月j日 H:i:s ' : 'n月j日 H:i ') : ($needSecond ? 'y年n月j日 H:i:s ' : 'y年n月j日 H:i');
		return date($format, $timestamp) . self::$WeekMap[date('w', $timestamp)];
	}
	/**
	 * 以人性化的方式显示时间
	 *
	 * @param integer $timestamp
	 * @param boolean $needSecond
	 * @param boolean &$recent
	 * @return string
	 */
	public static function detailDisplayDate($timestamp, $needSecond = true, &$recent = false) {
		$period = (int) (ceil(strtotime(date('Y-m-d', $timestamp)) / 86400) - ceil(strtotime(date('Y-m-d')) / 86400) + 2);
		$recent = $period;
		$format = $needSecond ? 'H:i:s' : 'H:i';
		$days = $period - 2;
		if (isset(self::$DayMap[$period])) {
			return self::$DayMap[$period] . ' ' . date($format, $timestamp);
		}
		elseif ($days > 2 && $days <= 30) {
			return $days . '天后 ' . date($format, $timestamp);
		}
		elseif ($days > 30) {
			return floor($days / 30) . '个月后 ' . date($format, $timestamp);
		}
		elseif ($days < -2 && $days >= -30) {
			return abs($days) . '天前 ' . date($format, $timestamp);
		}
		else if ($days < -30) {
			return abs(floor($days / 30)) . '个月前 ' . date($format, $timestamp);
		}
		return self::activityDisplayDate($timestamp, true);
	}
	/**
	 * 时间转换成秒
	 * 
	 * @param Integer $time
	 * @param String  $format   day、Hour、Minute、Second
	 * @return Integer
	 */
	public static function timeCommuteToSecond($timestamp, $format = "hour") {
		$toTime = 0;
		switch ($format) {
			case "day" :
				$toTime = $timestamp * 24 * 60 * 60;
				break;
			case "hour" :
				$toTime = $timestamp * 60 * 60;
				break;
			case "minute" :
				$toTime = $timestamp * 60;
				break;
			case "second" :
				$toTime = $timestamp;
				break;
			default :
				$toTime = $timestamp;
		}
		return $toTime;
	}
	
	/**
	 * 秒转时间转换
	 * @param Integer $time
	 * @return String
	 *
	 */
	public static function timeToDay($time) {
		$timeStr = "";
		switch ($time) {
			case $time > 24 * 60 * 60 :
				$timeStr = floor($time / (60 * 60 * 24)) . "天";
				break;
			case $time > 60 * 60 :
				$timeStr = floor($time / (60 * 60)) . "小时";
				break;
			case $time > 60 :
				$timeStr = floor($time / 60) . "分钟";
				break;
			case $time < 60 :
				$timeStr = floor($time) . "秒";
				break;
			default :
				$timeStr = floor($time) . "秒";
		}
		return $timeStr;
	}
	/**
	 * 一个月中有多少天
	 *
	 * @param integer $year
	 * @param integer $month
	 */
	public static function daysCountInMonth($year, $month) {
		return (int) date('d', strtotime('-1 day', strtotime('+1 month', mktime(0, 0, 0, $month, 1, $year))));
	}
	
	public static function formatTimeToString($time) {
		return date('Y年-m月-d日 H点i分', strtotime($time));
	}
	/**
	 * 时间差值,返回数组
	 *
	 * @param integer $d1
	 * @param integer $d2
	 * @return array
	 */
	public static function timeBetweenArray($d1, $d2) {
		$d = abs($d1 - $d2);
		$d = ($day = floor($d / 3600 / 24)) ? $d - $day * 3600 * 24 : $d;
		$d = ($hour = floor($d / 3600)) ? $d - $hour * 3600 : $d;
		$d = ($minute = floor($d / 60)) ? $d - $minute * 60 : $d;
		return array(
			'day' => $day, 
			'hour' => $hour, 
			'minute' => $minute, 
			'second' => $d
		);
	}
	/**
	 * 时间差值,返回字符串
	 *
	 * @param integer $d1
	 * @param integer $d2
	 * @return string
	 */
	public static function timeBetweenString($d1, $d2, $needSeconds = true) {
		$d = self::timeBetweenArray($d1, $d2);
		$ret = $d['day'] ? $d['day'] . '天' : '';
		$ret .= $d['hour'] ? $d['hour'] . '小时' : '';
		$ret .= $d['minute'] ? $d['minute'] . '分' : '';
		if ($needSeconds) {
			$ret .= $d['second'] ? $d['second'] . '秒' : '';
		}
		return $ret;
	}
}