如何在php中动态生成一些值? [关闭]

如何在php中动态生成一些值?  [关闭]

问题描述:

I want to store these values in an array

$hours = array("06:00", "06:15", "06:30"....etc );

The last value is 23:45

I wish it to be stored in ascending order.

My question is ... how I can automatically generate these values and add them in array?

If you manually add them .... they are many and it is not good.

You tell me please a method by which I could dynamically generate these values?

Thanks in advance!

我想将这些值存储在数组中 p>

  $  hours = array(“06:00”,“06:15”,“06:30”等等); 
  code>  pre> 
 
 

最后一个值为 23:45 code> p>

我希望它按升序存储。 p>

我的问题是......我怎么能 自动生成这些值并将它们添加到数组中? p>

如果你手动添加它们......它们很多而且不好。 p>

请告诉我一个可以动态生成这些值的方法吗? p>

提前致谢! p> div>

One way is this:

$hours = array(6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23);
$minutes = array('00','15','30','45');

$result_array = array();

foreach($hours as $h){
    foreach($minutes as $m){
        $val = str_pad($h,2,'0',STR_PAD_LEFT) . ':' . $m;
        $result_array[] = $val;
    }
}

var_dump($result_array);

LIVE EXAMPLE

Use two for loops. Outer one starts from 6 to 23. Inner one start from 0 to 45 incremented by 15. Join both with : . Problem solved

Let's get wild!

$date = \DateTime::createFromFormat('H:i', '06:00');
$stop_date = \DateTime::createFromFormat('H:i', '23:45');

$result = [];
do {
    $result[] = $date->format('H:i');
    $date->modify('+15 minutes');
} while($date <= $stop_date);

var_dump($result);

<?php
$timeArray=array(); 
for($i=6; $i <=23; $i++)
{
    for($j=0; $j<=45; $j++)
    {
        $timeArray[]= $i.':'.$j;
        $j= $j+14;
    }
}
echo "<pre>"; print_r($timeArray);
?>

This will Output

Array
(
    [0] => 6:0
    [1] => 6:15
    [2] => 6:30
    [3] => 6:45
    [4] => 7:0
    [5] => 7:15
    [6] => 7:30
    [7] => 7:45
    [8] => 8:0
    [9] => 8:15
    [10] => 8:30
    [11] => 8:45
    [12] => 9:0
    [13] => 9:15
    [14] => 9:30
    [15] => 9:45
    [16] => 10:0
    [17] => 10:15
    [18] => 10:30
    [19] => 10:45
    [20] => 11:0
    [21] => 11:15
    [22] => 11:30
    [23] => 11:45
    [24] => 12:0
    [25] => 12:15
    [26] => 12:30
    [27] => 12:45
    [28] => 13:0
    [29] => 13:15
    [30] => 13:30
    [31] => 13:45
    [32] => 14:0
    [33] => 14:15
    [34] => 14:30
    [35] => 14:45
    [36] => 15:0
    [37] => 15:15
    [38] => 15:30
    [39] => 15:45
    [40] => 16:0
    [41] => 16:15
    [42] => 16:30
    [43] => 16:45
    [44] => 17:0
    [45] => 17:15
    [46] => 17:30
    [47] => 17:45
    [48] => 18:0
    [49] => 18:15
    [50] => 18:30
    [51] => 18:45
    [52] => 19:0
    [53] => 19:15
    [54] => 19:30
    [55] => 19:45
    [56] => 20:0
    [57] => 20:15
    [58] => 20:30
    [59] => 20:45
    [60] => 21:0
    [61] => 21:15
    [62] => 21:30
    [63] => 21:45
    [64] => 22:0
    [65] => 22:15
    [66] => 22:30
    [67] => 22:45
    [68] => 23:0
    [69] => 23:15
    [70] => 23:30
    [71] => 23:45
)

Since you didn't explicitly state in your question, I assume you want to generate hours and minutes.

A simple way to do it is to generate the values as number of minutes since midnight ('06:00' is 60*6, '23:45' is 23*60+45) then format each value as "hour:minute" using 2-digit numbers left padded with 0:

$list = array_map(
    // format the values as hour:minute (2-digits, left padded with 0)
    function ($item) { 
        $min = $item % 60; $hour = ($item - $min) / 60;
        return sprintf('%02d:%02d', $hour, $min);
    },
    // generate the list of values as number of minutes since midnight
    range(6 * 60, 24 * 60 - 15, 15)
);

Another way to do it is to use DateTime objects:

$start = new DateTime('06:00');
$end   = new DateTime('23:45'); 
$step  = new DateInterval('PT15M');

$list = array();
for ($date = $start; $date <= $end; $date->add($step)) {
    $list[] = $date->format('H:i');
}

Please notice that this approach can fail if it runs on the day when the DST starts or ends. It can be easily fixed by adding a day when the DST doesn't change for sure in the strings used to initialize $start and $end:

$start = new DateTime('2016-01-01 06:00');
$end   = new DateTime('2016-01-01 23:45'); 

This function is based upon unix timestamp but returns the time in 24 hour format neatly.

function findtime($start,$end,$interval = '15 minutes'){
   $time = array(date('H:i',$start)); //Init array and first value
   $last = $start;  


   while($last < $end){ //Loops until we reach the end value
      $last = strtotime('+' . $interval,$last); //Generates the next value
      $time[] = date('H:i',$last); //Store the new value in the desired format
   }

   return $time; 
}