Print Time in 15-Minute Increments Between Two Times in the Same Day

Print time in 15-minute increments between two times in the same day

You need to move the last line outside of the outer for loop.

for ($i = 8; $i <= 16; $i++){
for ($j = 0; $j <= 45; $j+=15){
//inside the inner loop
echo_datelist($i, $j, $day, $month, $year);
}
//inside the outer loop
}
//outside the outer loop
echo_datelist(17, 0, $day, $month, $year);

In plain terms, you are saying:

For each hour between 8 and 16
For each 15 minute interval
Echo the time
End
Echo 17:00
End

Instead of:

For each hour between 8 and 16
For each 15 minute interval
Echo the time
End
End
Echo 17:00

I would consider performing your sql query for all hours of the day and then picking out the ones within the time from, otherwise you be doing an sql query for each 15 minute interval (37 queries with your sample data)

incrementing time 15 minutes within loop

You can use DateTime for that:

$now = new DateTime();
$end = clone $now;
$end->modify("+24 hours");

while ($now <= $end) {
echo "<option>" . $now->format('h:i A'). "</option>";
$now->modify('+15 minutes');
}

Number of 15 minutes intervals between two datetimes

from datetime import datetime, timedelta

now = datetime.now()
start = now + timedelta(minutes = 15)
finish = start + timedelta(minutes = 30)

elapsed = finish - start
number_of_intervals = elapsed / timedelta(minutes=15)

elapsed is the timedelta between start and finish. Divide by 15 minutes to calculate how many 15 minute intervals fit in there.

Note that this returns a float, so includes fractional intervals. Round as appropriate.

Generating 15 minute time interval array in python

Here's a generic datetime_range for you to use.

Code

from datetime import datetime, timedelta

def datetime_range(start, end, delta):
current = start
while current < end:
yield current
current += delta

dts = [dt.strftime('%Y-%m-%d T%H:%M Z') for dt in
datetime_range(datetime(2016, 9, 1, 7), datetime(2016, 9, 1, 9+12),
timedelta(minutes=15))]

print(dts)

Output

['2016-09-01 T07:00 Z', '2016-09-01 T07:15 Z', '2016-09-01 T07:30 Z', '2016-09-01 T07:45 Z', '2016-09-01 T08:00 Z', '2016-09-01 T08:15 Z', '2016-09-01 T08:30 Z', '2016-09-01 T08:45 Z', '2016-09-01 T09:00 Z', '2016-09-01 T09:15 Z', '2016-09-01 T09:30 Z', '2016-09-01 T09:45 Z' ... ]

Java - Create an array of times (15 minute) intervals between current time and a future set time

You're overwriting the time each cycle of the inner loop. You should use a List<String> instead and just append without worrying about indexes, like this:

String[] quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array

for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}

Generate 72 hours of timeslots with 15 minutes of interval in android

If you need to create "as many 15 minute slots" that can fit in a 72-hour time span, starting today at midnight... then I'd leverage the power of java.time apis:

This is naive pseudo-code, but you get the idea:

import java.time.*

fun main() {
val startTime = LocalTime.of(0, 0)
val today = LocalDate.now()
var current = LocalDateTime.of(today, startTime) //use datetime to account for day changes.
val endDateTime = current.plusHours(72)
val timeSlots = mutableListOf<LocalTime>()// or LocalDateTime if you need the "date" component as well.

timeSlots.add(current.toLocalTime()) // add the 1st interval

while (current.isBefore(endDateTime)) {
val newCurrent = current.plusMinutes(15)
timeSlots.add(newCurrent.toLocalTime())

current = newCurrent
}

println(timeSlots)
}

This prints:

[00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 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, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 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, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 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, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00]

You can try and play with this in the Kotlin playground.

Now if you want "day2" to print 24:00, 24:15, 24:30, 24:45, 25:00, etc., you'd likely want to keep it as a LocalDateTime and leverage the day comparison to know where in the sequence you are; I'm sure you can figure that part out, as you're able to perform date arithmetic operations with Java.Time apis quite easily.

Note this is not the only and perhaps not even the best way, but it's a way.

An alternative is to keep an increment and get startTime.plus(increment).
All in all, Java Time APIs are very straightforward.

Update

It appears you need a nudge to complete this, so I spent 15 minutes in the playground and came up with a very naive solution. Spoiler alert, there's no magic.

You can find the updated playground with this change here, and play with it.

I'm positive there could be a lot of optimizations and maybe even better ways to achieve this that are more functional. Hopefully the comments are self-explanatory.

invoke it with transform(list) and you get a List<String> that prints out as:

[00:00, 00:15 ...omitted for brevity... 24:00, 24:15, 24:30, 24:45, 25:00 ...omitted for brevity... 70:30, 70:45, 71:00, 71:15, 71:30, 71:45, 72:00]

Here's the "transform" function:

fun transform(source: MutableList<LocalDateTime>): List<String> {
val dayMultiplier = 24 //hours per day, duh!
val initialDay = source[0].toLocalDate()

val result = mutableListOf<String>()

source.forEach {
val time = it.toLocalTime()
val hour: Int = time.hour

// calculate the number of days between the dates
val daysBetween = ChronoUnit.DAYS.between(initialDay, it)
// Calculate the offset of hours based on the day difference
val hourOffset = daysBetween * dayMultiplier
//... and add them to the current hour.
val newHour = hour + hourOffset

// Add leading zeros (optional)
val reformattedHour = if (newHour < 10) "0$newHour" else newHour
val reformattedMinute = if (time.minute <10) "0${time.minute}" else time.minute

// Naively compose the new time
val newTime = "$reformattedHour:$reformattedMinute"

result.add(newTime)
}

return result
}

Update 2

  • I think using Duration is better (as seen in this answer here) by Basil Bourque.
  • This whole thing can (and likely should) be done in the same loop block. Since you naturally create your object there and put it in the list, nothing stops you from adding the extra step of adding the multiplier and storing the string directly. I'd still try to store the durations if the timeline is not tied to a date/time but rather time "intervals" (aka: durations).

The more complete data you save, the easier will be to "transform it" into whatever you need. If all you store from a ZonedDateTime is the number of minutes, for e.g., then you wouldn't be able to get the date from that... but if you saved the whole ZonedDateTime, you could get pretty much anything out of it.

Swift Minute intervals between two dates

Use the Calendar function date(byAdding:value:to:wrappingComponents:)

func dates(fromStart start: Date,
toEnd end: Date,
component: Calendar.Component,
value: Int) -> [Date] {
var result = [Date]()
var working = start
repeat {
result.append(working)
guard let new = Calendar.current.date(byAdding: component, value: value, to: working) else { return result }
working = new
} while working <= end
return result
}

Here is that code in a functioning playground:

import UIKit

func dates(fromStart start: Date,
toEnd end: Date,
component: Calendar.Component,
value: Int) -> [Date] {
var result = [Date]()
var working = start
repeat {
result.append(working)
guard let new = Calendar.current.date(byAdding: component, value: value, to: working) else { return result }
working = new
} while working <= end
return result
}

extension Date {
var localDate: String {return DateFormatter.localizedString(from: self, dateStyle: .medium, timeStyle: .medium)}
}

let threePMComponents = DateComponents(year: 2021, month: 6, day: 7, hour: 15)
guard let threePM = Calendar.current.date(from: threePMComponents) else {
fatalError()
}

let sixPMComponents = DateComponents(year: 2021, month: 6, day: 7, hour: 18)
guard let sixPM = Calendar.current.date(from: sixPMComponents) else {
fatalError()
}

print("Start time = \(threePM.localDate)")
print("End time = \(sixPM.localDate)")

let datesInRange = dates(fromStart: threePM, toEnd: sixPM, component: .minute, value: 15)
for item in datesInRange {
print(item.localDate)
}

The output is:

Start time = Jun 7, 2021 at 3:00:00 PM
End time = Jun 7, 2021 at 6:00:00 PM
Jun 7, 2021 at 3:00:00 PM
Jun 7, 2021 at 3:15:00 PM
Jun 7, 2021 at 3:30:00 PM
Jun 7, 2021 at 3:45:00 PM
Jun 7, 2021 at 4:00:00 PM
Jun 7, 2021 at 4:15:00 PM
Jun 7, 2021 at 4:30:00 PM
Jun 7, 2021 at 4:45:00 PM
Jun 7, 2021 at 5:00:00 PM
Jun 7, 2021 at 5:15:00 PM
Jun 7, 2021 at 5:30:00 PM
Jun 7, 2021 at 5:45:00 PM
Jun 7, 2021 at 6:00:00 PM

If you want the function to "snap" to the next even interval of the specified calendar component it would be more involved.

Generate and classify rows with 15 minutes time interval for a given time of the day

This should work (assuming TimeOfDay is TIME data type, otherwise you will need to convert). Rolling this into a computed column probably makes the most sense.

SELECT
FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,TimeOfDay),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,TimeOfDay),'hh\:mm')

If you want the intervals not to overlap (14:00 - 14:14, 14:15 - 14:29, etc) then you would just need to change it to

SELECT
FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,TimeOfDay),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,14-DATEPART(minute,@time)%15,TimeOfDay),'hh\:mm')

Do note that "24:00" is not a displayed value for TIME, if you want something like 23:45 - 24:00 you'd have to break apart the hour and minutes and stitch things together from there.

Test code:

DECLARE @time TIME;

SET @time = '01:00:00.000' --Should return 01:00 - 01:15

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '01:00:00.001' --Should return 01:00 - 01:15

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '01:14:59.999' --Should return 01:00 - 01:15

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '01:15:00.000' --Should return 01:15 - 01:30

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '13:45:00.000' --Should return 13:45 - 14:00

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '13:45:00.001' --Should return 13:45 - 14:00

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')

SET @time = '14:00:00.000' --Should return 14:00 - 14:15

SELECT FORMAT(DATEADD(minute,-1*DATEPART(minute,@time)%15,@time),'hh\:mm') + ' - ' + FORMAT(DATEADD(minute,15-DATEPART(minute,@time)%15,@time),'hh\:mm')


Related Topics



Leave a reply



Submit