1. <?php
  2. /**
  3. * Paginate timestamps for internal tooling.
  4. *
  5. * @param array $timestamps Array of timestamps to paginate. Each element should be a timestamp value.
  6. * @param int $page The current page number (1-based).
  7. * @param int $perPage The number of items per page.
  8. * @return array An array containing the paginated timestamps. Returns an empty array on error.
  9. */
  10. function paginateTimestamps(array $timestamps, int $page, int $perPage): array
  11. {
  12. if (empty($timestamps) || $page < 1 || $perPage <= 0) {
  13. return []; // Handle empty input or invalid parameters
  14. }
  15. $startIndex = ($page - 1) * $perPage;
  16. $endIndex = $startIndex + $perPage;
  17. if ($endIndex > count($timestamps)) {
  18. $endIndex = count($timestamps); // Ensure we don't go out of bounds
  19. }
  20. $paginatedTimestamps = array_slice($timestamps, $startIndex, $endIndex - $startIndex);
  21. return $paginatedTimestamps;
  22. }
  23. //Example Usage
  24. /*
  25. $timestamps = array(
  26. 1678886400,
  27. 1678972800,
  28. 1679059200,
  29. 1679145600,
  30. 1679232000,
  31. 1679318400,
  32. 1679404800,
  33. 1679491200,
  34. );
  35. $page = 2;
  36. $perPage = 2;
  37. $paginated = paginateTimestamps($timestamps, $page, $perPage);
  38. print_r($paginated);
  39. */
  40. ?>

Add your comment