ExampleTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. use Laravel\Lumen\Testing\DatabaseMigrations;
  3. use Laravel\Lumen\Testing\DatabaseTransactions;
  4. class ExampleTest extends TestCase
  5. {
  6. /**
  7. * A basic test example.
  8. *
  9. * @return void
  10. */
  11. public function testExample()
  12. {
  13. $this->get('/');
  14. \Illuminate\Support\Facades\Log::info('1212434');
  15. // $this->assertEquals(
  16. // $this->app->version(), $this->response->getContent()
  17. // );
  18. }
  19. /**
  20. * 获取上个月的开始和结束
  21. * @param int $ts 时间戳
  22. * @return array 第一个元素为开始日期,第二个元素为结束日期
  23. */
  24. protected function lastMonth($ts) {
  25. $ts = intval($ts);
  26. $oneMonthAgo = mktime(0, 0, 0, date('n', $ts) - 1, 1, date('Y', $ts));
  27. $year = date('Y', $oneMonthAgo);
  28. $month = date('n', $oneMonthAgo);
  29. return array(
  30. date('Y-m-1', strtotime($year . "-{$month}-1")),
  31. date('Y-m-t', strtotime($year . "-{$month}-1"))
  32. );
  33. }
  34. /**
  35. * 获取上n周的开始和结束,每周从周一开始,周日结束日期
  36. * @param int $ts 时间戳
  37. * @param int $n 你懂的(前多少周)
  38. * @param string $format 默认为'%Y-%m-%d',比如"2012-12-18"
  39. * @return array 第一个元素为开始日期,第二个元素为结束日期
  40. */
  41. protected function lastNWeek($ts, $n, $format = '%Y-%m-%d') {
  42. $ts = intval($ts);
  43. $n = abs(intval($n));
  44. // 周一到周日分别为1-7
  45. $dayOfWeek = date('w', $ts);
  46. if (0 == $dayOfWeek)
  47. {
  48. $dayOfWeek = 7;
  49. }
  50. $lastNMonday = 7 * $n + $dayOfWeek - 1;
  51. $lastNSunday = 7 * ($n - 1) + $dayOfWeek;
  52. return array(
  53. strftime($format, strtotime("-{$lastNMonday} day", $ts)),
  54. strftime($format, strtotime("-{$lastNSunday} day", $ts))
  55. );
  56. }
  57. public function testLast(){
  58. print_r($this->lastMonth(strtotime('2017-07-06')));
  59. print_r($this->lastNWeek(strtotime('2017-07-06'),1));
  60. echo date('Y-m-d', strtotime('this week'));
  61. echo date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
  62. }
  63. }