Получить данные для времени по timestamp

EndoCrinolog

Здравствуйте. У меня есть такая функция

PHP:
  1. function getCurrentDay($time)
  2. {
  3.     $response = array();
  4.     $response[‘year’] = date(«Y», $time);
  5.     $response[‘month’] = date(«m», $time);
  6.     $response[‘day’] = date(«d», $time);
  7.     $response[‘hour’] = date(«H», $time);
  8.     $response[‘minute’] = date(«i», $time);
  9.     $response[‘second’] = date(«s», $time);
  10.     $response[‘week_long’] = getDayRus( $time );
  11.     $response[‘week_short’] = getDayRusShort( $time );
  12.     $today_string = $response[‘year’] . ‘-‘ . $response[‘month’] . ‘-‘ . $response[‘day’];
  13.     $timestamp_today = strtotime($today_string);
  14.     $response[‘unix’] = $timestamp_today;
  15.     $response[‘full_unix’] = $time;
  16.  
  17.     $week_start = date(‘Y-m-d’, strtotime(‘last monday’, strtotime(‘tomorrow’)));
  18.     $week_end = date(‘Y-m-d’, strtotime(‘this sunday’));
  19.  
  20.     $month_start = date(«Y-m-1», strtotime(«first day of this month») );
  21.     $month_end = date(«Y-m-t», strtotime(«last day of this month») );
  22.  
  23.     $year_start = date(«Y-m-d», strtotime(«first day of january this year») );
  24.     $year_end = date(«Y-m-d», strtotime(«last day of december this year»));
  25.  
  26.     $response[‘begins’] = array(
  27.         ‘week’ => array(
  28.             ‘date’  =>  $week_start,
  29.             ‘unix’  =>  strtotime($week_start)
  30.         ),
  31.         ‘month’ =>  array(
  32.             ‘date’  =>  $month_start,
  33.             ‘unix’  =>  strtotime($month_start)
  34.         ),
  35.         ‘year’ =>  array(
  36.             ‘date’  =>  $year_start,
  37.             ‘unix’  =>  strtotime($year_start)
  38.         )
  39.     );
  40.     $response[‘ends’] = array(
  41.         ‘week’ => array(
  42.             ‘date’  =>  $week_end,
  43.             ‘unix’  =>  strtotime($week_end) + 86399
  44.         ),
  45.         ‘month’ =>  array(
  46.             ‘date’  =>  $month_end,
  47.             ‘unix’  =>  strtotime($month_end) + 86399
  48.         ),
  49.         ‘year’ =>  array(
  50.             ‘date’  =>  $year_end,
  51.             ‘unix’  =>  strtotime($year_end) + 86399
  52.         )
  53.     );
  54.  
  55.     $response[‘format’] = array(
  56.         ‘dd.mm.yyyy’  =>  $response[‘day’] . ‘.’ . $response[‘month’] . ‘.’ . $response[‘year’],
  57.         ‘dd.mm.yyyy hh:mm:ss’   =>  $response[‘day’] . ‘.’ . $response[‘month’] . ‘.’ . $response[‘year’] . ‘ ‘ . $response[‘hour’] . ‘:’ . $response[‘minute’] . ‘:’ . $response[‘second’]
  58.     );
  59.     return $response;
  60. }

Суть этой функции в том, чтобы получать данные по времени, который передастся в аргументе $time.

Примерная работа:

PHP:
  1. $YearUnix = strtotime($i . ‘-01-01’);
  2. $YearData = getCurrentDay($YearUnix);

То есть, получаю данные по указанному времени — что за день, часы, минуты и так далее..

Единственное, что я понял, так это то, что я неправильно сделал функцию получения начал и концов месяца и недели. Они берут ТЕКУЩУЮ дату, тогда как должны брать за основу ту, которая прилетает с аргументом. Я так понимаю, это происходит из-за того, что использует «семантику», типа «tomorrow», «last» и т.д.

Как правильно осуществить нахождение начал и концов промежутков для корректной выдачи результата? Заранее, спасибо!

 

Drunkenmunky

PHP:
  1. <?php
  2. $date_1 = ‘2021-02-01’;
  3. $date_2 = ‘2021-03-01’;
  4.  
  5. $utime_1 = date_format(date_create($date_1), ‘U’);
  6. $utime_2 = date_format(date_create($date_2), ‘U’);
  7.  
  8. ?>
 

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *