PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类

PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类:

全部方法:

01、返回今日开始和结束的时间戳。

02、返回昨日开始和结束的时间戳。

03、返回本周开始和结束的时间戳。

04、返回上周开始和结束的时间戳。

05、返回本月开始和结束的时间戳。

06、返回上个月开始和结束的时间戳。

07、返回今年开始和结束的时间戳。

08、返回去年开始和结束的时间戳。

09、获取几天前零点到现在、昨日结束的时间戳。

10、返回几天前的时间戳。

11、返回几天后的时间戳。

12、天数转换成秒数。

13、周数转换成秒数。

类源码:

[php]  view plain  copy
  1. <?php  
  2. class Time  
  3. {  
  4.     /** 
  5.      * 返回今日开始和结束的时间戳 
  6.      * 
  7.      * @return array 
  8.      */  
  9.     public static function today()  
  10.     {  
  11.         return [  
  12.             mktime(0, 0, 0, date('m'), date('d'), date('Y')),  
  13.             mktime(23, 59, 59, date('m'), date('d'), date('Y'))  
  14.         ];  
  15.     }  
  16.    
  17.     /** 
  18.      * 返回昨日开始和结束的时间戳 
  19.      * 
  20.      * @return array 
  21.      */  
  22.     public static function yesterday()  
  23.     {  
  24.         $yesterday = date('d') - 1;  
  25.         return [  
  26.             mktime(0, 0, 0, date('m'), $yesterdaydate('Y')),  
  27.             mktime(23, 59, 59, date('m'), $yesterdaydate('Y'))  
  28.         ];  
  29.     }  
  30.    
  31.     /** 
  32.      * 返回本周开始和结束的时间戳 
  33.      * 
  34.      * @return array 
  35.      */  
  36.     public static function week()  
  37.     {  
  38.         $timestamp = time();  
  39.         return [  
  40.             strtotime(date('Y-m-d'strtotime("this week Monday"$timestamp))),  
  41.             strtotime(date('Y-m-d'strtotime("this week Sunday"$timestamp))) + 24 * 3600 - 1  
  42.         ];  
  43.     }  
  44.    
  45.     /** 
  46.      * 返回上周开始和结束的时间戳 
  47.      * 
  48.      * @return array 
  49.      */  
  50.     public static function lastWeek()  
  51.     {  
  52.         $timestamp = time();  
  53.         return [  
  54.             strtotime(date('Y-m-d'strtotime("last week Monday"$timestamp))),  
  55.             strtotime(date('Y-m-d'strtotime("last week Sunday"$timestamp))) + 24 * 3600 - 1  
  56.         ];  
  57.     }  
  58.    
  59.     /** 
  60.      * 返回本月开始和结束的时间戳 
  61.      * 
  62.      * @return array 
  63.      */  
  64.     public static function month($everyDay = false)  
  65.     {  
  66.         return [  
  67.             mktime(0, 0, 0, date('m'), 1, date('Y')),  
  68.             mktime(23, 59, 59, date('m'), date('t'), date('Y'))  
  69.         ];  
  70.     }  
  71.    
  72.     /** 
  73.      * 返回上个月开始和结束的时间戳 
  74.      * 
  75.      * @return array 
  76.      */  
  77.     public static function lastMonth()  
  78.     {  
  79.         $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));  
  80.         $end = mktime(23, 59, 59, date('m') - 1, date('t'$begin), date('Y'));  
  81.    
  82.         return [$begin$end];  
  83.     }  
  84.    
  85.     /** 
  86.      * 返回今年开始和结束的时间戳 
  87.      * 
  88.      * @return array 
  89.      */  
  90.     public static function year()  
  91.     {  
  92.         return [  
  93.             mktime(0, 0, 0, 1, 1, date('Y')),  
  94.             mktime(23, 59, 59, 12, 31, date('Y'))  
  95.         ];  
  96.     }  
  97.    
  98.     /** 
  99.      * 返回去年开始和结束的时间戳 
  100.      * 
  101.      * @return array 
  102.      */  
  103.     public static function lastYear()  
  104.     {  
  105.         $year = date('Y') - 1;  
  106.         return [  
  107.             mktime(0, 0, 0, 1, 1, $year),  
  108.             mktime(23, 59, 59, 12, 31, $year)  
  109.         ];  
  110.     }  
  111.    
  112.     public static function dayOf()  
  113.     {  
  114.    
  115.     }  
  116.    
  117.     /** 
  118.      * 获取几天前零点到现在/昨日结束的时间戳 
  119.      * 
  120.      * @param int $day 天数 
  121.      * @param bool $now 返回现在或者昨天结束时间戳 
  122.      * @return array 
  123.      */  
  124.     public static function dayToNow($day = 1, $now = true)  
  125.     {  
  126.         $end = time();  
  127.         if (!$now) {  
  128.             list($foo$end) = self::yesterday();  
  129.         }  
  130.    
  131.         return [  
  132.             mktime(0, 0, 0, date('m'), date('d') - $daydate('Y')),  
  133.             $end  
  134.         ];  
  135.     }  
  136.    
  137.     /** 
  138.      * 返回几天前的时间戳 
  139.      * 
  140.      * @param int $day 
  141.      * @return int 
  142.      */  
  143.     public static function daysAgo($day = 1)  
  144.     {  
  145.         $nowTime = time();  
  146.         return $nowTime - self::daysToSecond($day);  
  147.     }  
  148.    
  149.     /** 
  150.      * 返回几天后的时间戳 
  151.      * 
  152.      * @param int $day 
  153.      * @return int 
  154.      */  
  155.     public static function daysAfter($day = 1)  
  156.     {  
  157.         $nowTime = time();  
  158.         return $nowTime + self::daysToSecond($day);  
  159.     }  
  160.    
  161.     /** 
  162.      * 天数转换成秒数 
  163.      * 
  164.      * @param int $day 
  165.      * @return int 
  166.      */  
  167.     public static function daysToSecond($day = 1)  
  168.     {  
  169.         return $day * 86400;  
  170.     }  
  171.    
  172.     /** 
  173.      * 周数转换成秒数 
  174.      * 
  175.      * @param int $week 
  176.      * @return int 
  177.      */  
  178.     public static function weekToSecond($week = 1)  
  179.     {  
  180.         return self::daysToSecond() * 7 * $week;  
  181.     }  
  182.    
  183.     private static function startTimeToEndTime()  
  184.     {  
  185.    
  186.     }  
  187. }  
  188.    
  189. ?>  

猜你喜欢

转载自blog.csdn.net/csflvcxx/article/details/80653177