1

can i get date time every minute in 2d array?i'm a beginner php developer.hope sme one can tell or share any idea.

let say my input as:

$startdate = 2013/7/01;
$enddate = 2013/7/02;

how can i have function as :

getMinuteRange($startdate, $enddate );

and i need the output of the function as:

Array (   
[0] => Array
    (
        [0] => 01/7/2013
        [1] => 12.00 a.m

    )

[1] => Array
    (
        [0] => 01/7/2013
        [1] => 12.01 a.m

    )

[2] => Array
    (
        [0] => 01/7/2013
        [1] => 12.03 a.m

    )
.
.
.

[2879] => Array
    (
        [0] => 02/7/2013
        [1] => 11.59 p.m

    )

)

zira
  • 117
  • 1
  • 1
  • 9
  • see if this helps: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Maximus2012 Jul 23 '13 at 03:18

2 Answers2

3
$begin = new DateTime('2013/07/01');
$end = new DateTime('2013/07/02');

$interval = new DateInterval('PT1M');//1 minute
$periods = new DatePeriod($begin, $interval, $end, DatePeriod::EXCLUDE_START_DATE);

$arr = iterator_to_array($periods);
print_r($arr);

That gives you an array of DateTime objects. you can use dateTime->format() to format the time strings how you prefer.

goat
  • 31,486
  • 7
  • 73
  • 96
2

This should work for you... it get the timestamp for the start and end timestamp and walks from one to the other incrementing by 60 seconds.

$startTime = "2013/07/01";
$endTime = "2013/07/02";

$startTS = strtotime($startTime);
$endTS = strtotime("tomorrow", strtotime($endTime));
$results = array();    
for($x = $startTS; $x<$endTS; $x+=60){ 
     $results[] = array(date("d/m/Y", $x), date("h.i a", $x));
}
if ($x != $endTS){
     $results[] = array(date("d/m/Y", $endTS), date("h.i a", $endTS));
}

print_r($results);
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • i just noticed those code will give date until 12.59pm on 1/7/2013.how i can adjust the code so if i choose start on 1/7/2013 and end 2/7/2013 so i get until 12.59pm on 2/7/2013 means 2880 minute(2 days time). – zira Jul 25 '13 at 02:54
  • 1
    change `$endTS = strtotime($endTime);` to `echo date("Y-m-d", strtotime("tomorrow", strtotime($endTime)));` – Orangepill Jul 25 '13 at 02:57
  • @zira Answer updated to reflect requested behavior – Orangepill Jul 25 '13 at 03:33