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;
}
Related
I am trying to get time interval with custom start and end time variables for which i have searched and find its relevant information on this link.
I have tried the following code but its giving errorUncaught Error: Call to a member function date() on string
$start = date("H:i",strtotime('08:30 AM'));
$end = date("H:i",strtotime('06:00 PM'));
for($i = $start; $i <= $end; $i->modify(' +30 MINUTE')){
echo $i->date("H:i");
}
You need to use the builtin \DateTime class and the \DateInterval class as well
$begin = new DateTime('08:30:00');
$end = new DateTime('12:45:00');
$interval = DateInterval::createFromDateString('30 minute');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("H:i\n");
}
RESULTS
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
And if the time roles over to another day something like this
$begin = new DateTimeImmutable('2022-03-01 16:00:00');
$end = (new DateTime())->createFromImmutable($begin)->add(new DateInterval('P1D'))->settime(8,30,0);
$interval = DateInterval::createFromDateString('30 minute');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("d/m/Y H:i\n");
}
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 have start time and end time in php.
If i give duration it should display all the time intervals.
Startime = 2014-07-28 07:00:00
End Time = 2014-07-28 11:00:00
duration = 30 min
i need output with 30 min difference between start and end time.
Output should look like :
07:00, 07:30 , 08:00 , 08:30 ..... 10:00 , 10:30 , 11:00
try
$s = strtotime("2014-07-28 07:00:00");
$e = strtotime("2014-07-28 11:00:00");
while($s != $e) {
$s = strtotime('+30 minutes', $s);
echo date('H:i', $s);
}
output :- 07:30 08:00 08:30 09:00 09:30 10:00 10:30 11:00
for comma seperated:-
while($s != $e) {
$s = strtotime('+30 minutes', $s);
$arr[] = date('H:i', $s);
}
echo implode(',', $arr);
output:- 07:30,08:00,08:30,09:00,09:30,10:00,10:30,11:00
Use the DatePeriod class for this:
$start = new DateTime('2014-07-28 07:00:00');
$end = new DateTime('2014-07-28 11:00:00');
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $time) {
echo $time->format('Y-m-d H:i:s') . PHP_EOL;
}
Output:
2014-07-28 07:00:00
2014-07-28 07:30:00
2014-07-28 08:00:00
2014-07-28 08:30:00
2014-07-28 09:00:00
2014-07-28 09:30:00
2014-07-28 10:00:00
2014-07-28 10:30:00
you can try
$start = "2014-07-28 07:00:00";
$end = "2014-07-28 11:00:00";
$start_time = strtotime($start);
$end_time = strtotime($end);
$time_diff = 30 * 60;
for($i=$start_time; $i<=$end_time; $i+=$time_diff)
{
echo date("H:i", $i).", ";
}
See WORKING DEMO
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/>";
}
?>
I would like to calculate some times. I need to know how many worktime hours are between a call entry and now regarding on the worktimes.
for example:
call stored yesterday 15:00
worktime ends: 18:00
worktime begins: 08:00
now: 10:00
So i need to know the age my call has during the working hours:
call stored >> worktime end: 3h
worktimebegin >> now: 2h
age: 5h
I would like to use php DateTime.
How would you proceed?
I think this can help you out:
//workdays till 18:00 and start 08:00
function calculateWorkHours(DateTime $Start, DateTime $End) {
//validate given parameters
if ($Start > $End) throw new Exception('$Start date cannot be later than $End date');
$hours = 0;
do {
//get the current hour
$currentHour = $Start->format('H');
//while the $currenthour is lower then 18 and higher than 7
if($currentHour < 18 && $currentHour >= 8) {
$hours++;
}
$Start->modify('+1 hour');
} while($End > $Start);
return $hours;
}
$Start = new DateTime('yesterday 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //returns 5
$Start = new DateTime('yesterday 060000');
$End = new DateTime('120000');
echo calculateWorkHours($Start, $End); //returns 14
$Start = new DateTime('-2 days 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //return 15