/    Sign up×
Community /Pin to ProfileBookmark

I would like to have a script that prints out a list of hours, starting from the current time until a defined time.
An example out put would look like this:
<button>09:00 – 10:00</button>
<button>11:00 – 12:00</button>
<button>13:00 – 14:00</button>
<button>15:00 – 16:00</button>
<button>17:00</button>
So the current time is 09:XX and the end time is 17:00.

The code I have so far is this:

[CODE]<?php
function create_time_range($start, $end, $by) {

$start_time = strtotime($start);
$end_time = strtotime($end);

$current = time();
$add_time = strtotime(‘+’.$by, $current);
$diff = $add_time-$current;

$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}

$time_now = date(“G”).”:00″;
$last_working_hour = date(’17:00′);

$times = create_time_range($time_now, $last_working_hour, ’60 mins’);

foreach ($times as $key => $time)
echo “<button class=’times’>”.date(‘G:i’, $time) . ‘</button> ‘;

?>[/CODE]

This produces:
<button>12:00</button>
<button>13:00</button>
<button>14:00</button>
<button>15:00</button>
<button>16:00</button>
<button>17:00</button>

Not in the grouping I want. Also if the time is 14:XX it should not produce:
<button>13:00 – 14:00</button>
<button>15:00 – 16:00</button>
<button>17:00</button>

But rather:
<button>14:00</button>
<button>15:00 – 16:00</button>
<button>17:00</button>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@happyharryauthorOct 07.2009 — Anyone have any ideas on this one?
×

Success!

Help @happyharry spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...