Skip to content

Instantly share code, notes, and snippets.

@juliensnz
Created March 16, 2016 16:18
Show Gist options
  • Save juliensnz/19d534d37c536fd36e7c to your computer and use it in GitHub Desktop.
Save juliensnz/19d534d37c536fd36e7c to your computer and use it in GitHub Desktop.
<?php
$messages = [
[
'start' => '11:00', // Datetime
'end' => '14:00', // Datetime
'interval' => '10', // display every x minutes
'text' => 'top', // The text to display
'duration' => 2 // duration of the display in minute
],
[
'start' => '10:00',
'end' => '12:00',
'interval' => '30',
'text' => 'toto',
'duration' => 10
],
];
function getMessageWithTime($message) {
$result = [];
$current = $message['start'];
while ($current < $message['end']) {
$result[] = array_merge($message, ['start_time' => $current, 'end_time' => $current + $message['duration']]);
$current = $current + $message['interval'];
}
return $result;
}
function getMessagesWithTime($messages) {
$result = [];
foreach ($messages as $message) {
$result = $result + getMessageWithTime($message);
}
return $result;
}
$result = getMessagesWithTime($messages);
$result = [
[
'start' => '10:00',
'end' => '12:00',
'interval' => '30',
'text' => 'toto',
'duration' => 10,
'start_time' => '10:00', //You have your start time
'end_time' => '10:01' //You have your end time
],
[
'start' => '10:00',
'end' => '12:00',
'interval' => '30',
'text' => 'toto',
'duration' => 10,
'start_time' => '10:30',
'end_time' => '10:31'
],
[
'start' => '10:00',
'end' => '12:00',
'interval' => '30',
'text' => 'toto',
'duration' => 10,
'start_time' => '11:00',
'end_time' => '11:01'
],
[
'start' => '10:00',
'end' => '12:00',
'interval' => '30',
'text' => 'toto',
'duration' => 10,
'start_time' => '11:30',
'end_time' => '11:31'
],
...
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment