297 lines
7.0 KiB
PHP
297 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: YSY3
|
|
* Date: 2019/1/8
|
|
* Time: 12:01
|
|
*/
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
class DateServices
|
|
{
|
|
public static $is_debug=0;
|
|
public static $times=1;
|
|
/**
|
|
* 返回日的开始和结束
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function startEndFormat($data){
|
|
|
|
$data=self::addTimes($data);
|
|
if(self::$is_debug)
|
|
{
|
|
dump($data);
|
|
}
|
|
return $data;
|
|
}
|
|
/**
|
|
* 多少分钟前到现在
|
|
* @param $number
|
|
* @return array
|
|
*/
|
|
public static function lastMinute($number,$field='start_at')
|
|
{
|
|
$last_minute =date('Y-m-d H:i:s',strtotime('-'.$number.' minute'));
|
|
$data= [
|
|
'start_at'=>$last_minute,
|
|
'end_at'=>date('Y-m-d H:i:s')
|
|
];
|
|
return $field?$data[$field]:$data;
|
|
|
|
}
|
|
|
|
/**
|
|
* 增加时间戳
|
|
*/
|
|
public static function addTimes($data){
|
|
|
|
if(self::$times)
|
|
{
|
|
$data= [
|
|
'start_at'=>$data['start_at'].' 00:00:00',
|
|
'end_at'=>$data['end_at'].' 23:59:59'
|
|
];
|
|
}
|
|
return $data;
|
|
}
|
|
public static function timeStartEndFormat($date){
|
|
$data= [
|
|
'start_at'=>$date,
|
|
'end_at'=>$date
|
|
];
|
|
return self::startEndFormat($data);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $date
|
|
* @return mixed
|
|
*/
|
|
public static function weekStartEndFormat($date){
|
|
$timestamp=strtotime($date);
|
|
$data['start_at']=date('Y-m-d', strtotime("this week Monday", $timestamp));
|
|
$data['end_at']=date('Y-m-d', strtotime("this week Sunday", $timestamp));
|
|
return self::startEndFormat($data);
|
|
|
|
}
|
|
|
|
/**
|
|
* 今天日期
|
|
* @return array
|
|
*/
|
|
public static function today()
|
|
{
|
|
$to_day=date('Y-m-d');
|
|
return self::timeStartEndFormat($to_day);
|
|
|
|
}
|
|
|
|
public static function dayRender($date,$day,$type){
|
|
return date('Y-m-d',strtotime($type.$day.' day',strtotime($date)));
|
|
}
|
|
public static function incrDay($date,$day=1){
|
|
$date=$date?$date:date('Y-m-d');
|
|
return self::dayRender($date,$day,'+');
|
|
}
|
|
public static function decrDay($date,$day=1){
|
|
$date=$date?$date:date('Y-m-d');
|
|
return self::dayRender($date,$day,'-');
|
|
}
|
|
|
|
/**
|
|
* 返回昨日开始和结束的日期
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function yesterday()
|
|
{
|
|
$yesterday =date('Y-m-d',strtotime('-1 day'));
|
|
return self::timeStartEndFormat($yesterday);
|
|
}
|
|
|
|
/**
|
|
* 返回周的开始日期和结束日期
|
|
* @param string $date
|
|
* @return mixed
|
|
*/
|
|
public static function week($date='')
|
|
{
|
|
|
|
$to_day=$date?$date:date('Y-m-d');
|
|
return self::weekStartEndFormat($to_day);
|
|
|
|
}
|
|
|
|
/**
|
|
* 返回上周开始和结束的日期
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function lastWeek($date='')
|
|
{
|
|
$date=$date?$date:date('Y-m-d');
|
|
$timestamp=strtotime($date);
|
|
$data['start_at']=date('Y-m-d', strtotime("last week Monday", $timestamp));
|
|
$data['end_at']=date('Y-m-d', strtotime("last week Sunday", $timestamp));
|
|
return self::startEndFormat($data);
|
|
}
|
|
|
|
/**
|
|
* 返回月开始和结束的
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function month($date='')
|
|
{
|
|
$date=$date?$date:date('Y-m-d');
|
|
$timestamp=strtotime($date);
|
|
$firstday = strtotime(date('Y-m-01', $timestamp));
|
|
$data['start_at']=date('Y-m-d', strtotime("0 month", $firstday));
|
|
$data['end_at']=date('Y-m-d', strtotime("+1 month -1 day", $firstday));
|
|
return self::startEndFormat($data);
|
|
|
|
}
|
|
|
|
/**
|
|
* 返回上个月开始和结束的时间戳
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function lastMonth($date='')
|
|
{
|
|
$date=$date?$date:date('Y-m-d');
|
|
$timestamp=strtotime($date);
|
|
$firstday = strtotime(date('Y-m-01', $timestamp));
|
|
$data['start_at']=date('Y-m-d', strtotime("-1 month", $firstday));
|
|
$data['end_at']=date('Y-m-d', strtotime("+0 month -1 day", $firstday));
|
|
return self::startEndFormat($data);
|
|
|
|
return [$begin, $end];
|
|
}
|
|
|
|
/**
|
|
* 返回年开始和结束的时间戳
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function year($date='')
|
|
{
|
|
$date=$date?$date:date('Y-m-d');
|
|
$timestamp=strtotime($date);
|
|
$firstday = strtotime(date('Y-01-01', $timestamp));
|
|
$data['start_at']=date('Y-m-d', strtotime("0 year", $firstday));
|
|
$data['end_at']=date('Y-m-d', strtotime("+1 year -1 day", $firstday));
|
|
return self::startEndFormat($data);
|
|
}
|
|
|
|
/**
|
|
* 返回去年开始和结束的时间戳
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function lastYear($date)
|
|
{
|
|
$date=$date?$date:date('Y-m-d');
|
|
$timestamp=strtotime($date);
|
|
$firstday = strtotime(date('Y-m-01', $timestamp));
|
|
$data['start_at']=date('Y-m-d', strtotime("-1 year", $firstday));
|
|
$data['end_at']=date('Y-m-d', strtotime("0 year -1 day", $firstday));
|
|
return self::startEndFormat($data);
|
|
}
|
|
|
|
public static function diffDay($date1,$date2=''){
|
|
|
|
$date2=$date2?$date2:date('Y-m-d');
|
|
if($date1>$date2){
|
|
$startTime = strtotime($date1);
|
|
$endTime = strtotime($date2);
|
|
}else{
|
|
$startTime = strtotime($date2);
|
|
$endTime = strtotime($date1);
|
|
}
|
|
$diff = $startTime-$endTime;
|
|
$day = $diff/86400;
|
|
|
|
return abs(intval($day));
|
|
}
|
|
|
|
public static function dayOf()
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取几天前零点到现在/昨日结束的时间戳
|
|
*
|
|
* @param int $day 天数
|
|
* @param bool $now 返回现在或者昨天结束时间戳
|
|
* @return array
|
|
*/
|
|
public static function dayToNow($day = 1, $now = true)
|
|
{
|
|
$end = time();
|
|
if (!$now) {
|
|
list($foo, $end) = self::yesterday();
|
|
}
|
|
|
|
return [
|
|
mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
|
|
$end
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 返回几天前的时间戳
|
|
*
|
|
* @param int $day
|
|
* @return int
|
|
*/
|
|
public static function daysAgo($day = 1)
|
|
{
|
|
$nowTime = time();
|
|
return $nowTime - self::daysToSecond($day);
|
|
}
|
|
|
|
/**
|
|
* 返回几天后的时间戳
|
|
*
|
|
* @param int $day
|
|
* @return int
|
|
*/
|
|
public static function daysAfter($day = 1)
|
|
{
|
|
$nowTime = time();
|
|
return $nowTime + self::daysToSecond($day);
|
|
}
|
|
|
|
/**
|
|
* 天数转换成秒数
|
|
*
|
|
* @param int $day
|
|
* @return int
|
|
*/
|
|
public static function daysToSecond($day = 1)
|
|
{
|
|
return $day * 86400;
|
|
}
|
|
|
|
/**
|
|
* 周数转换成秒数
|
|
*
|
|
* @param int $week
|
|
* @return int
|
|
*/
|
|
public static function weekToSecond($week = 1)
|
|
{
|
|
return self::daysToSecond() * 7 * $week;
|
|
}
|
|
|
|
private static function startTimeToEndTime()
|
|
{
|
|
|
|
}
|
|
|
|
} |