I am working on an appointment script that takes office opening and closing hours from database and then displays the time in between the opening and closing hours to book for a meeting, each meeting can only be booked for half an hour, so i need to display timings like this using a radio button
9:00am - 9:30am
9:30am - 10:00am
10:00am - 10:30am
...
...
...
I am able to calculate the total number of hours office is open, and using for loop i am able to display the timings
$office_start_time = '09:00:00';
$office_end_time = '16:00:00';
$start = explode(':', $office_start_time);
$end = explode(':', $office_end_time);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
echo 'total hours: '.$total_hours;
echo '<br>';
//exit;
?>
<table class="table table-hover table-bordered">
<tbody>
<?php for ($i = 0; $i < $total_hours; $i++) {
echo "<input name='time' type='radio' value='09:00|09:30'> 9:00am-9:30am<br/>";
?>
<?php } ?>
</tbody>
</table>
my problem is the above code gives me an output like below and I am stuck at how to go about getting the right outcome
I will really appreciate if i can get some help on displaying the timings as below
9:00am - 9:30am
9:30am - 10:00am
10:00am - 10:30am
...
...
...
$start = new DateTime('09:00:00');
$end = new DateTime('16:00:01'); // add 1 second because last one is not included in the loop
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
$previous = '';
foreach ($period as $dt) {
$current = $dt->format("h:ia");
if (!empty($previous)) {
echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>";
}
$previous = $current;
}
See it in action
strtotime's magic can do this
$start = '09:00:00';
$end = '16:00:00';
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
Result:
09:30 - 10:00
10:00 - 10:30
10:30 - 11:00
11:00 - 11:30
11:30 - 12:00
12:00 - 12:30
12:30 - 13:00
13:00 - 13:30
13:30 - 14:00
14:00 - 14:30
14:30 - 15:00
15:00 - 15:30
15:30 - 16:00
How about
<?php
for ($i = 0; $i < $total_hours; $i++)
{
$time = $start[0] + $i;
echo "<input name='time' type='radio' value='0" . $time . ":00|0" . $time . ":30'> " . $time . ":00am-" . $time . ":30am<br/>";
}
?>
Related
I've got the tutorial from this post.
The PHP code:
$start = '20:00:00';
$end = '07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
Now I need to show the time from 20:00:00 until tomorrow at 07:59:59.
Current code when I'm trying to run it got no result (empty).
The problem with your code is that it's just comparing the times not dates so obviously in your example the start time is greater than the end time.
Try the following example it works perfectly.
<?php
$TodayDate = date("Y-m-d");
$start = $TodayDate.' 20:00:00';
$TomorrowDate = new DateTime('tomorrow');
$TomorrowDate = $TomorrowDate->format('Y-m-d');
$end = $TomorrowDate.' 07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
?>
20:00 - 20:30
20:30 - 21:00
21:00 - 21:30
21:30 - 22:00
22:00 - 22:30
22:30 - 23:00
23:00 - 23:30
23:30 - 00:00
00:00 - 00:30
00:30 - 01:00
01:00 - 01:30
01:30 - 02:00
02:00 - 02:30
02:30 - 03:00
03:00 - 03:30
03:30 - 04:00
04:00 - 04:30
04:30 - 05:00
05:00 - 05:30
05:30 - 06:00
06:00 - 06:30
06:30 - 07:00
07:00 - 07:30
07:30 - 08:00
You could use the DateTime class with it's associated methods - add ~ this would perhaps simplify the task? It is simple to modify the above to work with a pre-defined start time / end time
$now = new DateTime();
$end = new DateTime( date( DATE_ATOM, strtotime( 'now + 1day' ) ) );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
To use the actual desired start/end times
$start = date( DATE_ATOM, strtotime( 'today 8pm' ) );
$finish = date( DATE_ATOM, strtotime( sprintf( '%s + 1day',$start ) ) );
$now = new DateTime( $start);
$end = new DateTime( $finish );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
Just add some date.
Search for 08:00:00 of which day?
$start = '01/01/2019 20:00:00';
$end = '01/02/2019 08:00:00';
$time = strtotime($start);
$timeStop = strtotime($end);
while ($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
I have x.
x is minutes.
I have a string start_time like: 7:00
And I have a string end_time like: 14:00
how can print a list of time with increase x minutes for each loop?
I want print something like this:
if x = 30
7:00 - 7:30
7:30 - 8:00
...
13:30 - 14:00
I try do it with math functions in php like this:
$time = '7:00';
$mm = $hh = 0;
$str = explode(":",$time);
if(($str[1]+ $x) > 60)
{
...
}
but it there a more simple method? can date function in php do it?
You can use DatePeriod together with DateInterval to achieve this.
Create two DateTime intervals with your start and end times, and a DateInterval instance with the number of minutes you need. Then, create a DatePeriod with this information, and iterate over it to show the resulting times:
<?php
$minutes = 15;
$start = "07:00";
$end = "14:00";
$startDate = DateTime::createFromFormat("H:i", $start);
$endDate = DateTime::createFromFormat("H:i", $end);
$interval = new DateInterval("PT".$minutes."M");
$dateRange = new DatePeriod($startDate, $interval, $endDate);
foreach ($dateRange as $date) {
echo $date->format("H:i")."<br>";
}
Demo
Result
07:00
07:15
07:30
07:45
08:00
08:15
08:30
08:45
09:00
09:15
09:30
09:45
10:00
10:15
// etc
You can use the DatePeriod object along with a desired DateInterval object.
Example: https://3v4l.org/lg8QW
$start = new \DateTime('07:00');
$end = new \DateTime('14:00');
$interval = new \DateInterval('PT1M'); //change to desired interval
$periods = new \DatePeriod($start, $interval, $end);
foreach ($periods as $period) {
echo $period->format('H:i') ;
}
Result:
07:00
07:01
07:02
07:03
07:04
07:05
07:06
07:07
07:08
07:09
07:10
07:11
07:12
07:13
07:14
07:15
07:16
07:17
07:18
07:19
07:20
//...
13:59
You can use DateTime object.
just like this:
<?php
$start_time = '7:00';
$date1 = DateTime::createFromFormat('H:i', $start_time);
$old = $start_time;
while (true) {
$date1->modify('+30 min');
echo $old . '-' . $date1->format('H:i').PHP_EOL;
$old = $date1->format('H:i');
if ($old == '14:00') {
break;
}
}
Output:
7:00-07:30
07:30-08:00
08:00-08:30
...
13:30-14:00
More info just see the manual here: DateTime
I want to create time slots with start,end time & also break timing.
public function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
$start = new DateTime($stTime);
$end = new DateTime($enTime);
$interval = new DateInterval("PT" . $duration. "M");
$period = new DatePeriod($start, $duration, $end);
foreach ($period as $dt) {
$periods[] = $dt->format('H:iA');
}
return $periods;
}
For ex.,
My service start time 10:00 AM , End Time 12:00 PM.
Condition: each service time 30 min & 15 min break.
Above method returns like,
10:00 AM - 10:30 AM
10:30 AM - 11:00 AM
11:00 AM - 11:30 AM
11:30 AM - 12:00 PM
Expected results as,
10:00 AM - 10:30 AM
10:45 AM - 11:15 AM
11:30 AM - 12:00 PM
I want to add break time when each period starts.
Thanks in advance.
How about this....
function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
$start = new DateTime($stTime);
$end = new DateTime($enTime);
$interval = new DateInterval("PT" . $duration. "M");
$breakInterval = new DateInterval("PT" . $break. "M");
for ($intStart = $start;
$intStart < $end;
$intStart->add($interval)->add($breakInterval)) {
$endPeriod = clone $intStart;
$endPeriod->add($interval);
if ($endPeriod > $end) {
$endPeriod=$end;
}
$periods[] = $intStart->format('H:iA') .
' - ' .
$endPeriod->format('H:iA');
}
return $periods;
}
I want to generate 3 time-period-list column between two times.
if
$start_time = '08:00 AM';
$end_time = '10:00: PM';
Time Period List As:
Morning ----- Noon ----- Eve
8:00 AM 1:00 PM 6:00 PM
8:30 AM 1:30 PM 6:30 PM
9:00 AM 2:.0 PM 7:00 PM
to to to
... ... ...
12:00 PM 5:00 PM 10:00 PM
I have calculated times between $start_time and $end_time as:
$time = time();
$rounded_time = $time % 900 > 450 ? $time += (900 - $time % 900): $time -= $time % 900;
$start = strtotime('08:00 AM');
$end = strtotime('10:00 PM');
for( $i = $start; $i <= $end; $i += 1800)
{
echo "<input name='start_time' type='radio' value='".date('g:i A', $i)."' />"." ".date('g:i A', $i)."<br>";
}
Remaining work to divide these times in three column as mention above
Thanks in advance to all my mates.
You can use strtotime method to add and substract time period from given time. For example:
$time = strtotime('10:00');
$halfAnHourBefore = date("H:i", strtotime('-30 minutes', $time));
$halfAnHourAfter = date("H:i", strtotime('+30 minutes', $time));
would give $halfAnHourBefore as 09:30 and $halfAnHourAfter as 10:30
I developed a function to obtain equal time intervals within a user generated time-frame.
$start = strtotime("12:00"); //start at...
$end = strtotime("18:00"); //end at...
$timeframe = $end - $start; //time-frame
$intervals = 3; //number of intervals within time-frame
$interval_time = $timeframe/$intervals; //time of each interval
for($i = 0, $start;
$i < $intervals;
$i++, $start = strtotime("+$interval_time seconds", $start)) //increment time
{
$new_time = date('H:i:s a', $start);
echo "$new_time \n";
}
The code above outputs
12:00:00 pm 14:00:00 pm 16:00:00 pm
What code can I incorporate to divide each interval and obtain the middle? For example to get from the code above
13:00:00 pm 15:00:00 pm 17:00:00 pm
Please kindly advise :)
Replace your for loop with:
$half_interval = $interval_time / 2;
$mid = $start + $half_interval;
for ( $i = 1; $i < $intervals; $i ++) {
echo date('H:i:s a', $mid) . " \n";
$mid += $interval_time;
}
echo date('H:i:s a', $mid) . " \n";
Admittedly a simple approach. I'm sure improvements are possible, but leave them as an exercise for the reader! ;-)