<?php
namespace App\common;
use DateTime;
use think\exception\InvalidArgumentException;
class Timer {
/**
* 返回N年后的时间戳,传入第二个参数,则从该时间开始计算
* @param int $year 年数(默认为1年)
* @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间)
* @param bool $round 是否取整(默认false),如果传入true,则返回当前日期1月1号0点的时间戳
* @return int 时间戳
*/
function afterYear($year = 1, $datetime = null, $round = false)
{
$date = new DateTime();
if ($datetime !== null) {
$date->setTimestamp(self::toTimestamp($datetime));
}
$timestamp = $date->modify(sprintf('+%d year', $year))->getTimestamp();
return $round ? strtotime(date('Y-1-1 00:00:00', $timestamp)) : $timestamp;
}
/**
* 返回两个日期相差年数(如果只传入一个日期,则与当前时间比较)
* @param int|string $datetime 要计算的时间
* @param int|string $new_datetime 要比较的时间(默认为当前时间)
* @return int 相差年数
*/
function diffYears($datetime, $new_datetime = null)
{
$datetime = date('Y-m-d', self::toTimestamp($datetime));
if ($new_datetime) {
$new_datetime = date('Y-m-d', self::toTimestamp($new_datetime));
} else {
$new_datetime = date('Y-m-d');
}
return date_diff(date_create($datetime), date_create($new_datetime))->y;
}
/**
* 返回两个日期相差天数(如果只传入一个日期,则与当天时间比较)
* @param int|string $datetime 要计算的时间
* @param int|string $new_datetime 要比较的时间(默认为当前时间)
* @return int 相差天数
*/
public static function diffDays($datetime, $new_datetime = null): int
{
$datetime = date('Y-m-d', self::toTimestamp($datetime));
if ($new_datetime) {
$new_datetime = date('Y-m-d', self::toTimestamp($new_datetime));
} else {
$new_datetime = date('Y-m-d');
}
return date_diff(date_create($datetime), date_create($new_datetime))->days;
}
/**
* 返回两个日期相差小时数(如果只传入一个日期,则与当天时间比较)
* @param int|string $datetime 要计算的时间
* @param int|string $new_datetime 要比较的时间(默认为当前时间)
* @return int 相差小时数
*/
public static function diffHour($datetime, $new_datetime = null): int
{
$datetime = date('Y-m-d H:i:s', self::toTimestamp($datetime));
if ($new_datetime) {
$new_datetime = date('Y-m-d H:i:s', self::toTimestamp($new_datetime));
} else {
$new_datetime = date('Y-m-d H:i:s');
}
$diff = date_diff(date_create($datetime), date_create($new_datetime));
return self::hourDay($diff->d) + $diff->h;
}
/**
* 返回两个日期相差分钟数(如果只传入一个日期,则与当天时间比较)
* @param int|string $datetime 要计算的时间
* @param int|string $new_datetime 要比较的时间(默认为当前时间)
* @return int 相差分钟数
*/
public static function diffMinute($datetime, $new_datetime = null): int
{
$datetime = date('Y-m-d H:i:s', self::toTimestamp($datetime));
if ($new_datetime) {
$new_datetime = date('Y-m-d H:i:s', self::toTimestamp($new_datetime));
} else {
$new_datetime = date('Y-m-d H:i:s');
}
$diff = date_diff(date_create($datetime), date_create($new_datetime));
return self::minuteDay($diff->d) + self::minuteHour($diff->h) + $diff->i;
}
/**
* 返回两个日期相差秒数(如果只传入一个日期,则与当天时间比较)
* @param int|string $datetime 要计算的时间
* @param int|string $new_datetime 要比较的时间(默认为当前时间)
* @return int 相差秒数
*/
public static function diffSecond($datetime, $new_datetime = null): int
{
$datetime = date('Y-m-d H:i:s', self::toTimestamp($datetime));
if ($new_datetime) {
$new_datetime = date('Y-m-d H:i:s', self::toTimestamp($new_datetime));
} else {
$new_datetime = date('Y-m-d H:i:s');
}
$diff = date_diff(date_create($datetime), date_create($new_datetime));
return self::secondDay($diff->d) + self::secondHour($diff->h) + self::secondMinute($diff->i) + $diff->s;
}
/**
* 将任意时间类型的参数转为时间戳
* 请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。
* @param int|string $datetime 要转换为时间戳的字符串或数字,如果为空则返回当前时间戳
* @return int 时间戳
*/
public static function toTimestamp($datetime = null): int
{
if (empty($datetime)) {
return time();
}
$start = strtotime('1970-01-01 00:00:00');
$end = strtotime('2099-12-31 23:59:59');
//判断是否为时间戳
if (is_numeric($datetime) && $datetime <= $end && $datetime >= $start) {
return intval($datetime);
} else {
$timestamp = strtotime($datetime);
if ($timestamp) {
return $timestamp;
} else {
throw new InvalidArgumentException('Param datetime must be a timestamp or a string time');
}
}
}
/**
* 返回一分钟的秒数,传入参数可以返回数分钟的秒数
* @param int $minutes 分钟数,默认为1分钟
* @return int 秒数
*/
public static function secondMinute(int $minutes = 1): int
{
return 60 * $minutes;
}
/**
* 返回一小时的秒数,传入参数可以返回数小时的秒数
* @param int $hours 小时数,默认为1小时
* @return int 秒数
*/
public static function secondHour(int $hours = 1): int
{
return self::secondMinute(60) * $hours;
}
/**
* 返回一天的秒数,传入参数可以返回数天的秒数
* @param int $days 天数,默认为1天
* @return int 秒数
*/
public static function secondDay(int $days = 1): int
{
return self::secondHour(24) * $days;
}
/**
* 返回一小时的分钟数,传入参数可以返回数小时的秒数
* @param int $hours 小时数,默认为1小时
* @return int 分钟数
*/
public static function minuteHour(int $hours = 1): int
{
return 60 * $hours;
}
/**
* 返回一天的分钟数,传入参数可以返回数天的秒数
* @param int $days 天数,默认为1天
* @return int 分钟数
*/
public static function minuteDay(int $days = 1): int
{
return self::minuteHour(24) * $days;
}
/**
* 返回一天的小时数,传入参数可以返回数天的秒数
* @param int $days 天数,默认为1天
* @return int 小时数
*/
public static function hourDay(int $days = 1): int
{
return 24 * $days;
}
/**
* 返回指定之前几天的时间戳(天)
* @param int $days
* @return int
*/
public static function beforeDay(int $days = 1): int
{
return strtotime(date('Y-m-d', strtotime('-'.$days.' day')));
}
/**
* 返回指定之后几天的时间戳(天)
* @param int $days
* @return int
*/
public static function afterDay(int $days = 1): int
{
return strtotime(date('Y-m-d', strtotime('+'.$days.' day')));
}
}
时间类库(PHP)
最新推荐文章于 2024-10-09 11:23:49 发布