【问题标题】:Data Structure for keeping time and day of the week PHP用于保持时间和星期几的数据结构 PHP
【发布时间】:2015-03-24 23:35:28
【问题描述】:

如何在 PHP 中的数据结构中只保存一天中的时间和星期几?

例如:周日 17:20

我还需要添加分钟和小时。

例如:周日 22:03 + 03:00 = 周一 01:03

【问题讨论】:

  • 您从阅读手册开始。这是其中的“一个”php.net/manual/en/function.date.php
  • 可能最好的方法是创建一个自定义类
  • 我使用date() 和strtotime() 在6 分钟多一点的时间内在phpfiddle(一个在线PHP 编辑器)中重新创建了这两个案例。访问这些链接并在 PHPFiddle 中尝试一下。如果您仍然遇到问题,请发布您尝试过的内容,我相信人们会很乐意提供帮助。根据help center SO,通常解决“特定编程问题”。这个问题目前的形式有点过于宽泛了。
  • 那是给朋友的,所以我不能再提供信息,除非我从他们那里得到更多信息......
  • @RaduStanescu 没错,但同样,您可以访问链接并使用date()、time() 和strtotime() 重新创建上面的示例。有关这些功能的文档非常出色。如果您能够重新创建示例,那么您将能够帮助您的朋友。

标签: php data-structures dayofweek


【解决方案1】:

未经测试,可能不是最好的方法

class TimeWeekday {
    private $hour;
    private $minute;
    private $day;

    private $days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

    public function __construct($hour, $minute, $day) {
        $this->hour = $hour;
        $this->minute = $minute;
        $this->day = $day;
    }

    public function add($hours, $minutes, $days = 0) {
        $newMinutes = $this->minute + $minutes;
        $this->minute = $newMinutes % 60;
        if ($this->minute < 0) {
            $this->minute += 60;
        }
        $newHours = $this->hour + $hours + floor($newMinutes / 60);
        $this->hour = $newHours % 24;
        if ($this->hour < 0) {
            $this->hour += 24;
        }
        $newDay = $this->day + $days + floor($newHours / 60);
        $this->day = $newDay % 7;
        if ($this->day < 0) {
            $this->day += 7;
        }
    }

    public function substract($hours, $minurtes, $days = 0) {
        $this->add(-$hours, -$minurtes, -$days);
    }

    public function getValue() {
        return sprintf('%02d:%02d %s', $this->hour, $this->minute, $this->days[$this->day]);
    }
}

.

$x = new TimeWeekday(22, 3, 6);
$x->add(3,0);
echo $x->getValue();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2015-08-19
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多