-2

I am trying to generate trip listing for a bus transit system. I am provided with a bus route's service start time, no of runs per day and frequency.

Reference

E.g- If the first run is at 05:00, the next run is at 06:00, 07:00 & son on up to 7 runs. I am stuck at generating the departure & arrival time range array as shown below.

Array
(
    [0] => 05:00-06:00
    [1] => 06:00-07:00
    [2] => 07:00-08:00
    [3] => 08:00-09:00
    [4] => 09:00-10:00
    [5] => 10:00-11:00
)

I am a PHP fresher & have less idea about date and time concept in PHP. So need help on this.

  • please show the code of where you split array – Ahmed Ali Feb 13 '20 at 08:40
  • 1
    Can we see the code you're stuck with? If you want to learn more about date in PHP you could read https://www.php.net/manual/en/function.date.php or https://www.php.net/manual/en/datetime.add.php. Also, 0 to 5 are only 6 trips – brombeer Feb 13 '20 at 08:41
  • Possible Duplicate [link 1](https://stackoverflow.com/questions/8169139/adding-minutes-to-date-time-in-php) / [link 2](https://stackoverflow.com/questions/20557059/php-adding-15-minutes-to-time-value) – Empty Brain Feb 13 '20 at 08:48

1 Answers1

0
$start = date('H:i', strtotime( $_POST['START'] ));
$frequenzy = date( 'g', strtotime( $_POST['FREQUENZY'] ));
$trips = $_POST['TRIPS'];

$time_array = array();

for( $i = 0; $i <= $trips; $i++ )  {
  $end = date( 'H:i', strtotime( $start . "+$frequenzy hour" ) );
  $time_array[] = "$start-$end";

  $start = $end;
}

I think that will help you. You only have to adjust the $_POST placeholder.

Robin Gillitzer
  • 1,603
  • 1
  • 6
  • 17