当前位置: 首页 > news >正文

网站建设在线推广推广代理公司

网站建设在线推广,推广代理公司,网站建设的开源平台,网站怎么做双语种在开发中,我们往往需要对数据进行统计,而统计离不开时间维度的搜索,因此我们可以建立一个公共的时间类库来生成相关的时间范围。class StatisticsDate{//环比对应的时间戳const CHAIN_RATIO [today > yesterday,week > lastWeek,month…

在开发中,我们往往需要对数据进行统计,而统计离不开时间维度的搜索,因此我们可以建立一个公共的时间类库来生成相关的时间范围。

class StatisticsDate

{

//环比对应的时间戳

const CHAIN_RATIO = [

'today' => 'yesterday',

'week' => 'lastWeek',

'month' => 'lastMonth'

];

//单例模式

protected static $instance;

//返回的时间格式

private $timestamp;

//单例方法

public static function getInstance()

{

if (!self::$instance instanceof StatisticsDate) {

self::$instance = new static();

}

return self::$instance;

}

//设置时间格式 默认为时间戳, 可传入 date 方法的 时间参数

public function setTimestamp($timestamp)

{

$this->timestamp = $timestamp;

return $this;

}

/**

* 传入时间格式 并返回开始时间和结束时间

* @param $dateType

* @return array

*/

public final function execute($dateType)

{

switch ($dateType)

{

case 'today':

$range = $this->getDateRangeByToday();

break;

case 'yesterday':

$range = $this->getDateRangeByYesterday();

break;

case 'week':

$range = $this->getDateRangeByWeek();

break;

case 'lastWeek':

$range = $this->getDateRangeByLastWeek();

break;

case 'month':

$range = $this->getDateRangeByMonth();

break;

case 'lastMonth':

$range = $this->getDateRangeByLastMonth();

break;

default:

$range = [];

}

return $range;

}

/**

* 环比时可以用

* @param $dateType

* @return array

*/

public function chainRatioExecute($dateType)

{

if(!isset(self::CHAIN_RATIO[$dateType]))

{

return [];

}

return $this->execute(self::CHAIN_RATIO[$dateType]);

}

/**

* 返回今天的时间范围

* @return array

*/

protected function getDateRangeByToday()

{

return [

$this->getTime(mktime(0, 0, 0, date('m'), date('d'), date('Y'))),

$this->getTime(mktime(23, 59, 59, date('m'), date('d'), date('Y')))

];

}

/**

* 返回昨日的时间范围

* @return array

*/

protected function getDateRangeByYesterday()

{

$yesterday = date('d') - 1;

return [

$this->getTime(mktime(0, 0, 0, date('m'), $yesterday, date('Y'))),

$this->getTime(mktime(23, 59, 59, date('m'), $yesterday, date('Y')))

];

}

/**

* 获取本周的时间范围

* @return array

*/

protected function getDateRangeByWeek()

{

$timestamp = time();

return [

$this->getTime(strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)))),

$this->getTime(strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1)

];

}

/**

* 获取上周的时间范围

* @return array

*/

protected function getDateRangeByLastWeek()

{

$timestamp = time();

return [

$this->getTime(strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp)))),

$this->getTime(strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1)

];

}

/**

* 获取本月的时间范围

* @return array

*/

protected function getDateRangeByMonth()

{

return [

$this->getTime(mktime(0, 0, 0, date('m'), 1, date('Y'))),

$this->getTime(mktime(23, 59, 59, date('m'), date('t'), date('Y')))

];

}

/**

* 获取上月的时间范围

* @return array

*/

protected function getDateRangeByLastMonth()

{

$begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));

$end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));

return [$this->getTime($begin), $this->getTime($end)];

}

/**

* 按 $this->timestamp 返回时间格式

* @param string $time

* @return false|int|string

*/

public function getTime($time = '')

{

switch (true) {

case (empty($time)):

$timeInt = time();

break;

case (is_numeric($time)):

$timeInt = $time;

break;

case (is_string($time)):

$timeInt = strtotime($time);

if (false == $timeInt) {

$timeInt = time();

}

break;

default:

$timeInt = time();

}

return empty($this->timestamp) ? $timeInt : date($this->timestamp, (int)$timeInt);

}

/**

* 获取给定的开始时间和结束时间的每天时间

* @param int $start

* @param int $end

* @return array|bool

*/

public function getTimeRangeByDateRange(int $start, int $end)

{

if($start > $end)

{

return false;

}

$diff = ($end - $start)/86400;

$day = [];

for ($i = 0; $i <= $diff; $i ++)

{

$day[] = $this->getTime($start + $i* 24 * 60 * 60);

}

return $day;

}

}

使用实例:

StatisticsDate::getInstance()->setTimestamp('Y-m-d')->execute('week');

http://www.lbrq.cn/news/2798605.html

相关文章:

  • 投票网站设计百度官网app下载
  • 游戏网页代码西安百度seo
  • 宁波哪家公司做网站好百度云盘登录入口
  • 响应式网站是怎么做的seo的基本内容
  • 建站节沈阳黄页88企业名录
  • 西安做门户网站最好的公司运营推广
  • 珠海市住房城乡建设官网北京seo公司wyhseo
  • 成都app制作软件seo教程免费
  • 建设摩托车是名牌吗关键词优化到首页怎么做到的
  • 靠谱网站优化哪家好交换友链
  • 网页设计需求模板seo营销服务
  • 住房新建网站在线crm系统
  • 医院网站建设方案书360指数在线查询
  • wordpress做学校网站合肥网站排名推广
  • 我先做个网站怎么做的百度快速排名优化技术
  • 怎样制作微信网站优化设计答案五年级上册
  • 建站的步骤有哪些在线培训网站
  • 网站建设需要注意哪些百度优化教程
  • 微信显示wordpress南京seo外包
  • 北京城市建设档案馆网站网站优化seo怎么做
  • dw怎么做网站首页广州seo排名优化服务
  • 赣州建站一手app推广接单平台
  • 东西湖区网站建设公司网络营销策划需要包括哪些内容
  • 一站式企业服务提高工作效率
  • 公司网站建设方案模板下载郑州网络推广方案
  • 成都怎样制作公司网站四川网络推广seo
  • 开发公司 网站建设kol合作推广
  • 做3d打印网站百度竞价ocpc
  • 淘宝网站建设好评语seo的内容主要有哪些方面
  • 建设食品网站搜索引擎优化指南
  • 【51单片机】【protues仿真】基于51单片机水位监测系统
  • C语言基础习题——01
  • 教育场景下禁用html5播放器拖动进度条的例子
  • CentOS7安装部署PostgreSQL
  • Python核心技术开发指南(001)——Python简介
  • Git Commit 提交信息标准格式