Solved

Loop through interval of 5 minutes between two dates

Posted on 2010-09-08
10
1,179 Views
Last Modified: 2012-05-10
Hi,

I have a start date and end date. Let's say 08:15 and 17:00

I need to loop through the time between these two dates, by 5 minutes interval.

So the loop should give in this case:

08:15
08:20
08:25
08:30
08:35
08:40
etc. until
17:00

(any dates like 8:15 should be 0-padded to have 08:15)
(the start date and end date are multiple of 5, I will never have something like 08:13)

Thank you
0
Comment
Question by:matthew016
10 Comments
 

Assisted Solution

by:nitinbabbar
nitinbabbar earned 50 total points
ID: 33627178
If you want to do excute time triggered code (specially with PHP) you need to install a cron job server.
There are a few free cron job provider (Maybe you ask Dr. Google :) )
Here you can find a little tutorial:
http://www.webmasters-central.com/article-blog/tutorials/cron-tutorial-managing-cron-tab-or-cron-job-is-easy/

Greets
0
 
LVL 9

Author Comment

by:matthew016
ID: 33627285
This has nothing to do with Cron tasks.
0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33628139
Ok, i assume the dates are in unix timestamp format. Otherwise you need to use mktime or strtotime functions.
$start=time();
$end=time()+10000;

while($start<$end) {
   $start=(floor($start/300)+1)*300;
   echo date('H:i', $start);
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33628177
Example that fits you needs totally:
$start=strtotime('8:15');
$end=strtotime('17:00');

while($start<$end) {
	$start=ceil($start/300)*300;
	$start++;
	echo date('H:i', $start), "\n";
}

Open in new window

0
 
LVL 2

Assisted Solution

by:johnwarde
johnwarde earned 50 total points
ID: 33628712
Maybe I've read your requirements incorrectly but you did mention dates. This script cycles through you time interval and the date range, probably more than you need .  Would have been simpler if  DateInterval->days worked properly.

John
function isGreaterThanDate($objDateInterval) {
    if ($objDateInterval->invert == 0 &&
        $objDateInterval->y > 0 ||
        $objDateInterval->m > 0 ||
        $objDateInterval->d > 0) {
        return true;
    }
    return false;
}

$objStartDate = new DateTime('2010-02-15');
$objEndDate   = new DateTime('2010-04-15');
$objIntervalOneDay = new DateInterval('P1D');
$objEndDate->add($objIntervalOneDay);

while (isGreaterThanDate(date_diff($objStartDate, $objEndDate))) {
    $intStartTime = strtotime('8:15');
    $intEndTime = strtotime('17:00');
    while($intStartTime <= $intEndTime) {
        echo $objStartDate->format('Y-m-d ').date('H:i', $intStartTime), "<br/>\r\n";
        $intStartTime += 300;
        flush();
    }
    $objStartDate->add($objIntervalOneDay);
}

Open in new window

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33628774
If you need dates too:
$start=strtotime('29.11.2011 8:15');
$end=strtotime('30.11.2011 17:00');

while($start<$end) {
	$start=ceil($start/300)*300;
	$start++;
	echo date('d.m.Y \a\t H:i', $start), "\n";
}

Open in new window

0
 
LVL 12

Assisted Solution

by:Rok-Kralj
Rok-Kralj earned 200 total points
ID: 33628813
@John: Your scipt fails bigtime if start time is let's say 8:12.

Mine works fine on all conditions.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 33630401
PHP has some fairly sturdy built-in functions for this sort of thing.  Please see my article here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
<?php // RAY_temp_5minutes.php
error_reporting(E_ALL);
echo "<pre>";



function intervals($alpha, $omega, $interval="5 MINUTES")
{
    // NORMALIZE THE DATA
    $alpha = date('c', strtotime($alpha));
    $omega = date('c', strtotime($omega));

    // CREATE THE ARRAY OF INTERVALS
    $intvl = array();
    while ($alpha <= $omega)
    {
        $intvl[] = date("H:i", strtotime($alpha));
        $alpha = date('c', strtotime($alpha . " + $interval"));
    }
    return $intvl;
}



// SOME TEST DATA
echo PHP_EOL . var_dump(intervals("0800", "0830"));
echo PHP_EOL . var_dump(intervals("0800", "0803"));

echo PHP_EOL . var_dump(intervals("0802", "0829"));
echo PHP_EOL . var_dump(intervals("0805", "0905", "15 MINUTES"));


echo PHP_EOL . var_dump(intervals("Today 23:50:00", "Tomorrow 00:03:15", "1 MINUTE"));

Open in new window

0
 
LVL 12

Assisted Solution

by:Rok-Kralj
Rok-Kralj earned 200 total points
ID: 33630669
@Ray: You seem to love strtotime(); :)
0
 
LVL 9

Author Closing Comment

by:matthew016
ID: 33758779
Thank you !
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question