php 按时间/日期间隔的最新页面 - Processwire

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 按时间/日期间隔的最新页面 - Processwire相关的知识,希望对你有一定的参考价值。

<?php
/**
 *  Get latest pages based on specified time interval
 *
 *  @param array $params            - Main Params Array
 *
 *  @param string|integer $value    - number of minutes|hours|days
 *  @param string $interval         - minutes|hours|days
 *  @param string $timezone         - optional time zone eg: Europe/Belgrade
 *  @param string $by               - created|modified - get pages by created or modified date
 *  @param string $selector         - selector to filter results
 *
 *	We need this dateTimeDiff() function to calculate difference between two dates
 *	@see https://gist.github.com/kreativan/bfbfab54bf237503b99454a9e1f5473f
 *
 *	@example getLatestPages([
 *              "selector" => "template=basic-page",
 *              "value" => "45","
 *              "interval" => "days",
 *              "by" => "modified",
 *          ]);
 *
 *  @return PageArray - return pages created under specified time interval
 */
function getLatestPages($params) {

    $value      = !empty($params["value"]) ? $params["value"] : "60";
    $interval   = !empty($params["interval"]) ? $params["interval"] : "minutes";
    $timezone   = !empty($params["timezone"]) ? $params["timezone"] : wire("config")->timezone;
    $by         = !empty($params["by"]) ? $params["by"] : "created";
    $selector   = !empty($params["selector"]) ? ",".$params["selector"] : ",limit=10"; // limit results to 10 by default

    // set timezone, default = $config->timezone
    if (!empty($timezone)) date_default_timezone_set("$timezone");

    // get latest pages
    $latest_arr = wire("pages")->find("sort=-{$by} $selector");

    // Create new page array.
    // And add only pages that are created in last x ($value) minutes/hours/days
    $latest_pages = new PageArray();
    foreach($latest_arr as $p) {
		
		// Add specified $value to the curent date/time
        $now_plus = strtotime("now +{$value} {$interval}");
        
        /**
         *	Get date difference in specified interval days|hours|minutes
         *	@var $target_time - target time in specified interval
         *	@var $created_time - page created date in specified interval 
         *	@see https://gist.github.com/kreativan/bfbfab54bf237503b99454a9e1f5473f
         */ 
        $target_time	= dateTimeDiff(time(), $now_plus, "$interval");
        $page_time      = dateTimeDiff(time(), $p->{$by}, "$interval");
		
		// Create new PageArray to store pages that meets our criteria
		// If $created_time is lower (lesser?) then $target_time,
		// page meets the criteria and will be added to the new PageArray
        if($page_time <= $target_time) {
            $latest_pages->add($p);
        }

    }

    return($latest_pages);

}


// Example
// Get pages created in last 45 days with template basic-page, and title or headline that contain "Processwire" word
$latest = getLatestPages([
    "value" => "45",
    "interval" => "days",
    "by" => "modified",
    "selector" => "template=basic-page, title|headline~=Processwire",
]);

print_r($latest);
<?php
/**
 *  Calculate difference between two dates
 *  and get the results in specified format
 *  @param integer $date1 - timestamp
 *  @param integer $date2 - timestamp
 *  @param string $format - days|years|months|hours|minutes
 *  @return integer
 */
function dateTimeDiff($date1, $date2, $format = "days") {

    $date1 = date("Y-m-d H:i:s", (int)$date1);
    $date2 = date("Y-m-d H:i:s", (int)$date2);

    $start_date = new \DateTime($date1);
    $end_date   = new \DateTime($date2);
    $diff = $start_date->diff($end_date);

    switch ($format) {
        case 'days':
            $result = $diff->days;
            break;
        case 'years':
            $result = $diff->y;
            break;
        case 'months':
            $result = $diff->m;
            break;
        case 'hours':
            $result = $diff->days * 24;
            break;
        case 'minutes':
            $result = $diff->days * 24 * 60;
            $result += $diff->h * 60;
            $result += $diff->i;
            break;
        default:
            $result = $diff->d;
            break;
    }

    return (int)$result;

}

以上是关于php 按时间/日期间隔的最新页面 - Processwire的主要内容,如果未能解决你的问题,请参考以下文章

PHP时间间隔至少十天

在日期范围内按日期聚合数据,结果集中没有日期间隔

如何按期间间隔选择/排序日期?

使用 JPA 的 Criteria API 按日期间隔分组

PHP在两个不同的日期和时间检查日期和时间间隔

MySQL 按日期查询大间隔