SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get all times between 2 times

    Hi guys


    How can I return all the times between the below with a 60 minute interval.

    2009-07-18 08:00:00
    2009-07-18 17:00:00

    So like this

    2009-07-18 08:00:00

    2009-07-18 09:00:00

    2009-07-18 10:00:00

    2009-07-18 11:00:00

    2009-07-18 12:00:00

    2009-07-18 13:00:00

    2009-07-18 14:00:00

    2009-07-18 15:00:00

    2009-07-18 16:00:00

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wazo View Post
    Hi guys

    How can I return all the times between the below with a 60 minute interval.
    Maybe this can help:

    PHP Code:
    <?php

    $start 
    mktime(8007182009);
    $end mktime(17007182009);
    $interval = (int) 60 60;

    for (
    $i $start$i $end$i += $interval) {
        echo 
    date('Y-m-d H:i:s'$i), '<br />';
    }

    ?>
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    As usual, there are a number of ways to skin this cat (far more than I care to post!) but here are three basic approaches that you might want to take a look at and move ahead with.

    Give this prefix to each of the scripts
    PHP Code:
    <?php header('Content-Type: text/plain;charset=utf-8'); error_reporting(E_ALL E_STRICT);

    $start  '2009-07-18 08:00:00';
    $end    '2009-07-18 17:00:00';
    $format 'Y-m-d H:i:s';
    $step   '+1 hour';

    date_default_timezone_set('Europe/London');
    Old-school date/strtotime
    A more traditional approach using a simple for loop and adding an hour on each iteration with strtotime.
    Aside: None of these snippets increment the timestamp directly (+3600 seconds).
    PHP Code:
    for ($t strtotime($start), $e strtotime($end); $t $e$t strtotime($step$t))
    {
        echo 
    date($format$t) . "\n";

    OOPsie Goodness
    This snippet makes use of the DateTime class available to us as of PHP 5.2 (and "experimentally" in 5.1). The format('U') returns a string containing the Unix timestamp.
    PHP Code:
    echo "\n\nUsing DateTime (PHP 5.2)\n\n"
    $time  = new DateTime($start);
    $limit = new DateTime($end);
    $limit = (int) $limit->format('U');
    do
    {
        echo 
    $time->format($format) . "\n";
        
    $time->modify($step);
    }
    while ((int) 
    $time->format('U') < $limit); 
    More OOPsie Goodness
    PHP 5.3 brought along with it a number of yummy additions in the form for DateInterval and DatePeriod, so lets use them. Hopefully the code should speak for itself, it's dead easy to understand.
    PHP Code:
    echo "\n\nUsing DateTime/DatePeriod/DateInterval (PHP 5.3)\n\n";
    $start    = new DateTime($start);
    $end      = new DateTime($end);
    $interval DateInterval::createFromDateString($step);
    $period   = new DatePeriod($start$interval$end);

    foreach (
    $period as $time)
    {
        echo 
    $time->format($format) . "\n";

    Summary
    There's three ways, fristi gave you another and no doubt someone else will have yet another way to do this!

    Off Topic:

    Why does it take so long for me to write and format posts?
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •