用于保存时间和星期几的数据结构 PHP
Data Structure for keeping time and day of the week PHP
如何在 PHP 中的数据结构中只保存一天中的时间和星期几?
例如:17:20星期日
我还需要添加分钟和小时。
例如:22:03 周日 + 03:00 = 01:03 周一
未经测试,可能不是最好的方法
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();
如何在 PHP 中的数据结构中只保存一天中的时间和星期几?
例如:17:20星期日
我还需要添加分钟和小时。
例如:22:03 周日 + 03:00 = 01:03 周一
未经测试,可能不是最好的方法
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();