<?php
namespace app\admin\service;
class TimeService {
public static function TimeBeginEnd($type = "today", $date = true, $format = "Y-m-d H:i:s") {
switch (strtolower($type)) {
case "tomorrow":
$arr = [self::BeginTomorrow($date, $format), self::EndTomorrow($date, $format)];
break;
case "yesterday":
$arr = [self::BeginYesterday($date, $format), self::EndYesterday($date, $format)];
break;
case "thisweek":
$arr = [self::BeginThisWeek($date, $format), self::EndThisWeek($date, $format)];
break;
case "thisweekr":
$arr = [self::BeginThisWeekR($date, $format), self::EndThisWeekR($date, $format)];
break;
case "lastweek":
$arr = [self::BeginLastWeek($date, $format), self::EndLastWeek($date, $format)];
break;
case "thismonth":
$arr = [self::BeginThisMonth($date, $format), self::EndThisMonth($date, $format)];
break;
case "lastmonth":
$arr = [self::BeginLastMonth($date, $format), self::EndLastMonth($date, $format)];
break;
case "thisseason":
$arr = [self::BeginThisSeason($date, $format), self::EndThisSeason($date, $format)];
break;
case "lastseason":
$arr = [self::BeginLastSeason($date, $format), self::EndLastSeason($date, $format)];
break;
case "thisyear":
$arr = [self::BeginThisYear($date, $format), self::EndThisYear($date, $format)];
break;
case "tomorrowyear":
$arr = [self::BeginTomorrowYear($date, $format), self::EndTomorrowYear($date, $format)];
break;
case "lastyear":
$arr = [self::BeginLastYear($date, $format), self::EndLastYear($date, $format)];
break;
default:
$arr = [self::BeginToday($date, $format), self::EndToday($date, $format)];
break;
}
return $arr;
}
public static function BeginToday($date = true, $format = "Y-m-d H:i:s") {
$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
return $date ? date($format, $today) : $today;
}
public static function EndToday($date = true, $format = "Y-m-d H:i:s") {
$today = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
return $date ? date($format, $today) : $today;
}
public static function BeginTomorrow($date = true, $format = "Y-m-d H:i:s") {
$Tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('Y')) + 86400;
return $date ? date($format, $Tomorrow) : $Tomorrow;
}
public static function EndTomorrow($date = true, $format = "Y-m-d H:i:s") {
$Tomorrow = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) + 86399;
return $date ? date($format, $Tomorrow) : $Tomorrow;
}
public static function BeginYesterday($date = true, $format = "Y-m-d H:i:s") {
$Yesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
return $date ? date($format, $Yesterday) : $Yesterday;
}
public static function EndYesterday($date = true, $format = "Y-m-d H:i:s") {
$Yesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
return $date ? date($format, $Yesterday) : $Yesterday;
}
public static function BeginThisWeek($date = true, $format = "Y-m-d H:i:s") {
$ThisWeek = strtotime("this week Monday", time());
return $date ? date($format, $ThisWeek) : $ThisWeek;
}
public static function EndThisWeek($date = true, $format = "Y-m-d H:i:s") {
$ThisWeek = strtotime("this week Sunday", time()) + 86399;
return $date ? date($format, $ThisWeek) : $ThisWeek;
}
public static function BeginThisWeekR($date = true, $format = "Y-m-d H:i:s") {
$ThisWeek = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
return $date ? date($format, $ThisWeek) : $ThisWeek;
}
public static function EndThisWeekR($date = true, $format = "Y-m-d H:i:s") {
$ThisWeek = mktime(0, 0, 0, date("m"), date("d") - date("w") + 7, date("Y"));
return $date ? date($format, $ThisWeek) : $ThisWeek;
}
public static function BeginLastWeek($date = true, $format = "Y-m-d H:i:s") {
$LastWeek = strtotime("last week Monday", time());
return $date ? date($format, $LastWeek) : $LastWeek;
}
public static function EndLastWeek($date = true, $format = "Y-m-d H:i:s") {
$LastWeek = strtotime("last week Sunday", time()) + 86399;
return $date ? date($format, $LastWeek) : $LastWeek;
}
public static function BeginThisMonth($date = true, $format = "Y-m-d H:i:s") {
$ThisMonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
return $date ? date($format, $ThisMonth) : $ThisMonth;
}
public static function EndThisMonth($date = true, $format = "Y-m-d H:i:s") {
$ThisMonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
return $date ? date($format, $ThisMonth) : $ThisMonth;
}
public static function BeginLastMonth($date = true, $format = "Y-m-d H:i:s") {
$LastMonth = mktime(0, 0, 0, date("m") - 1, 1, date("Y"));
return $date ? date($format, $LastMonth) : $LastMonth;
}
public static function EndLastMonth($date = true, $format = "Y-m-d H:i:s") {
$LastMonth = mktime(23, 59, 59, date("m"), 0, date("Y"));
return $date ? date($format, $LastMonth) : $LastMonth;
}
public static function BeginThisSeason($date = true, $format = "Y-m-d H:i:s") {
$season = ceil((date('n')) / 3);
$ThisSeason = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'));
return $date ? date($format, $ThisSeason) : $ThisSeason;
}
public static function EndThisSeason($date = true, $format = "Y-m-d H:i:s") {
$season = ceil((date('n')) / 3);
$ThisSeason = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'));
return $date ? date($format, $ThisSeason) : $ThisSeason;
}
public static function BeginLastSeason($date = true, $format = "Y-m-d H:i:s") {
$season = ceil((date('n')) / 3) - 1;
$ThisSeason = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'));
return $date ? date($format, $ThisSeason) : $ThisSeason;
}
public static function EndLastSeason($date = true, $format = "Y-m-d H:i:s") {
$season = ceil((date('n')) / 3) - 1;
$ThisSeason = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'));
return $date ? date($format, $ThisSeason) : $ThisSeason;
}
public static function BeginThisYear($date = true, $format = "Y-m-d H:i:s") {
$ThisYear = mktime(0, 0, 0, 1, 1, date('Y'));
return $date ? date($format, $ThisYear) : $ThisYear;
}
public static function EndThisYear($date = true, $format = "Y-m-d H:i:s") {
$ThisYear = mktime(23, 59, 59, 12, 31, date('Y'));
return $date ? date($format, $ThisYear) : $ThisYear;
}
public static function BeginTomorrowYear($date = true, $format = "Y-m-d H:i:s") {
$TomorrowYear = mktime(0, 0, 0, 1, 1, date('Y') + 1);
return $date ? date($format, $TomorrowYear) : $TomorrowYear;
}
public static function EndTomorrowYear($date = true, $format = "Y-m-d H:i:s") {
$TomorrowYear = mktime(23, 59, 59, 12, 31, date('Y') + 1);
return $date ? date($format, $TomorrowYear) : $TomorrowYear;
}
public static function BeginLastYear($date = true, $format = "Y-m-d H:i:s") {
$LastYear = mktime(0, 0, 0, 1, 1, date('Y') - 1);
return $date ? date($format, $LastYear) : $LastYear;
}
public static function EndLastYear($date = true, $format = "Y-m-d H:i:s") {
$LastYear = mktime(23, 59, 59, 12, 31, date('Y') - 1);
return $date ? date($format, $LastYear) : $LastYear;
}
}