Moment on a Timetable
Write a class Time to represent the time which you often see on a
timetable. Your class should implement the following methods:
- __construct($string)
Creates a time with the given string. The format of the string is
"hh:mm", where hh represents hours from 00 to 23, and mm represents
minutes from 00 to 59.
This constructor should initialize the instance variables
$hour
and
$minute
to store the input time.
- modify($d)
Shifts the time by d minutes. If $d is negative, the time
is changed to a previous moment. For example, "13:15" becomes "11:45"
after it is shifted -90 minutes.
- delta($d)
Similar to modify(). However,
delta() return an object with the
shifted time but not modifying the object itself,
in contrast with modify() which
modifies the object without returning anything.
- getTime()
Returns a string that shows the time with the correct format, e.g.,
"09:10", "23:55".
You may test your class with the following main program.
<?php
$departure = new Time("22:54");
$arrival = new Time("23:36");
print "Your train departs at\t\t" . $departure->getTime();
print " and arrives at" . $arrival->getTime() . "<br />\n";
$departure->modify(-39);
$arrival->modify(-39);
print "The previous train departs at\t\t" . $departure->getTime();
print " and arrives at" . $arrival->getTime() . "<br />\n";
$departure->modify(99);
$arrival->modify(99);
print "The next train departs at\t\t" . $departure->getTime();
print " and arrives at" . $arrival->getTime() . "<br />\n";
?>
The result may look like
Your train departs at 22:54 and arrives at 23:36
The previous train departs at 22:15 and arrives at 22:57
The next train departs at 23:54 and arrives at 00:36