You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PHP日期系统中的一天从24小时改为5小时?

Is it possible to adapt PHP date functions to a 5-hour day?

Great question! This is totally achievable, but you’ll need to move beyond PHP’s native DateTime class since it’s hardcoded to follow the 24-hour day convention. The key is to build a custom wrapper that maps your 5-hour day rules to standard time units, and handles edge cases like wrapping over to the next "custom day" when time exceeds your 8AM-1PM window.

Core Rules to Implement

First, let’s formalize the rules from your examples:

  • 1 "custom day" = 5 standard hours, aligned to standard calendar days (runs from 8AM to 1PM of the same standard date)
  • Adding custom hours wraps over to the next standard date’s 8AM window if it exceeds 1PM
  • Adding a custom day jumps directly to the same time (8AM) on the next standard date

Example Implementation: CustomDateTime Class

Here’s a simplified custom class that encapsulates PHP’s DateTime and implements your rules:

class CustomDateTime {
    private $standardDateTime;
    // Define your custom day boundaries (standard time)
    const CUSTOM_DAY_START = 8; // 8 AM
    const CUSTOM_DAY_LENGTH = 5; // 5 hours total

    public function __construct($timeString) {
        $this->standardDateTime = new DateTime($timeString);
    }

    // Helper: Calculate how many hours into the current custom day we are
    private function getCustomDayOffset() {
        $startOfCustomDay = (clone $this->standardDateTime)
            ->setTime(self::CUSTOM_DAY_START, 0, 0);
        
        $offsetSeconds = $this->standardDateTime->getTimestamp() - $startOfCustomDay->getTimestamp();
        
        // Handle times before 8AM (treat as part of the previous custom day)
        if ($offsetSeconds < 0) {
            $offsetSeconds += self::CUSTOM_DAY_LENGTH * 3600;
            $this->standardDateTime->modify('-1 day');
        }

        return $offsetSeconds / 3600;
    }

    // Custom modify method for your rules
    public function modify($intervalString) {
        // Handle "+N day" requests
        if (preg_match('/\+(\d+) day/', $intervalString, $matches)) {
            $days = (int)$matches[1];
            // Jump to the same custom start time on the next N standard days
            $this->standardDateTime
                ->modify("+$days day")
                ->setTime(self::CUSTOM_DAY_START, 0, 0);
        }
        // Handle "+N hours" requests
        elseif (preg_match('/\+(\d+) hour/', $intervalString, $matches)) {
            $hoursToAdd = (int)$matches[1];
            $currentOffset = $this->getCustomDayOffset();
            $totalOffset = $currentOffset + $hoursToAdd;

            // Calculate how many custom days we need to jump, and remaining hours
            $customDaysToAdd = floor($totalOffset / self::CUSTOM_DAY_LENGTH);
            $remainingHours = $totalOffset % self::CUSTOM_DAY_LENGTH;

            // Update the standard date/time
            $this->standardDateTime
                ->modify("+$customDaysToAdd day")
                ->setTime(
                    self::CUSTOM_DAY_START + $remainingHours,
                    $this->standardDateTime->format('i'),
                    $this->standardDateTime->format('s')
                );
        }

        return $this;
    }

    // Proxy to standard DateTime format method
    public function format($format = 'Y-m-d H:i:s') {
        return $this->standardDateTime->format($format);
    }
}

Test Your Examples

Let’s verify the scenarios you mentioned:

// Test 1: +1 custom day from 2017-10-14 08:00
$date1 = new CustomDateTime('2017-10-14 08:00');
$date1->modify('+1 day');
echo $date1->format(); // Output: 2017-10-15 08:00 ✅

// Test 2: +3 custom hours from 2017-10-14 11:00
$date2 = new CustomDateTime('2017-10-14 11:00');
$date2->modify('+3 hours');
echo $date2->format(); // Output: 2017-10-15 09:00 ✅

Next Steps to Expand

This is a basic implementation—you can extend it further to:

  • Support negative intervals (e.g., -2 days, -1 hour)
  • Handle more complex modify syntax (e.g., +1 week, next month—though you’d need to define what "week" means in your 5-hour day system)
  • Add validation for invalid times (e.g., times that don’t fit your custom day structure)
  • Fully implement the DateTimeInterface for better compatibility with PHP’s date functions

内容的提问来源于stack exchange,提问作者Ahmad Samilo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:56:52