<?php
/**
* Paginate timestamps for internal tooling.
*
* @param array $timestamps Array of timestamps to paginate. Each element should be a timestamp value.
* @param int $page The current page number (1-based).
* @param int $perPage The number of items per page.
* @return array An array containing the paginated timestamps. Returns an empty array on error.
*/
function paginateTimestamps(array $timestamps, int $page, int $perPage): array
{
if (empty($timestamps) || $page < 1 || $perPage <= 0) {
return []; // Handle empty input or invalid parameters
}
$startIndex = ($page - 1) * $perPage;
$endIndex = $startIndex + $perPage;
if ($endIndex > count($timestamps)) {
$endIndex = count($timestamps); // Ensure we don't go out of bounds
}
$paginatedTimestamps = array_slice($timestamps, $startIndex, $endIndex - $startIndex);
return $paginatedTimestamps;
}
//Example Usage
/*
$timestamps = array(
1678886400,
1678972800,
1679059200,
1679145600,
1679232000,
1679318400,
1679404800,
1679491200,
);
$page = 2;
$perPage = 2;
$paginated = paginateTimestamps($timestamps, $page, $perPage);
print_r($paginated);
*/
?>
Add your comment