How to insert the start datetime and enddate time in laravel - php

I want to insert startdatetime and end date time time.
table availability
id
userid
startdatetime
enddatetime
1
2
2023-01-27 10:00:00
2023-01-27 12:00:00
1
2
2023-01-28 10:00:00
2023-01-28 12:00:00
1
2
2023-01-29 10:00:00
2023-01-29 12:00:00
1
2
2023-01-30 10:00:00
2023-01-30 12:00:00
1
2
2023-01-31 10:00:00
2023-01-31 12:00:00
1
2
2023-02-01 10:00:00
2023-02-01 12:00:00
Continuing on till 2023-02-25 12:00:00.
The start date time will be 2023-01-27 10:00:00 and end date time 2023-02-25 12:00:00.
for($i = $begin; $i <= $end; $i->modify('+1 day'))
{
$_attribute = array(
'start_date_time' => $i->format('Y-m-d H:i:s'),
'end_date_time' => $i->format('Y-m-d H:i:s'),
'user_id' => $user->id
);
\App\models\availability::create($_attribute);
}

You can use CarbonPeriod instead of for loop
$begin = Carbon::parse('2023-01-27');
$end = Carbon::parse('2023-03-25');
$dates = CarbonPeriod::create($being, $end)->toArray();
$now = now(); // needed for created_at and updated_at
$attributes = [];
foreach ($dates as $date) {
$attributes[] = [
'start_date_time' => $date->hour(10)->minute(0)->seconds(0),
'end_date_time' => $date->hour(12)->minute(0)->seconds(0),
'user_id' => $user->id,
// must add these because we are using insert()
'created_at' => $now,
'updated_at' => $now,
];
}
\App\models\availability::insert($attributes);

Related

Need 10 intervals between two time stamps

I need 10 intervals between two time stamps. Please find the example below.
start_date: 2021-01-15 15:45:00
end_date: 2021-01-15 18:00:00
I need result like below based on count of intervals. I can change 5 interval or 7 intervals but the time need to calculate automatically based on intervals
15:45:00
16:00:00
16:15:00
16:30:00
16:45:00
17:00:00
17:15:00
17:30:00
17:45:00
18:00:00
function SplitTime($StartTime, $EndTime, $Duration="60"){
$ReturnArray = array ();// Define output
$StartTime = strtotime ($StartTime); //Get Timestamp
$EndTime = strtotime ($EndTime); //Get Timestamp
$AddMins = $Duration * 60;
while ($StartTime <= $EndTime) //Run loop
{
$ReturnArray[] = date ("G:i:s", $StartTime);
$StartTime += $AddMins; //Endtime check
}
return $ReturnArray;
}
You can use a division of the number of seconds between the 2 dates :
function SplitTime($StartTime, $EndTime, $Division = 10){
$ReturnArray = array (); // Define output
$StartTime = strtotime ($StartTime); //Get Timestamp
$EndTime = strtotime ($EndTime); //Get Timestamp
//Time diff
$diff = $EndTime - $StartTime;
// number of seconds by "step"
$num = $diff / ($Division - 1);
for ($inum = 0; $inum < $Division; $inum++)
{
$ReturnArray[] = date ("G:i:s", $StartTime + $num * $inum);
}
return $ReturnArray;
}
$dates = SplitTime("2021-01-15 15:45:00", "2021-01-15 18:00:00");
print_r($dates);
Outputs :
Array
(
[0] => 15:45:00
[1] => 16:00:00
[2] => 16:15:00
[3] => 16:30:00
[4] => 16:45:00
[5] => 17:00:00
[6] => 17:15:00
[7] => 17:30:00
[8] => 17:45:00
[9] => 18:00:00
)
See online demo

Convert times into interval array

If I have the following variables:
$starttime = '09:00'; // Start time
$endtime = '21:00'; // End time
$interval = '30'; // In minutes
What would be the best way to generate the following array?
[
"09:00" => "09:30",
"09:30" => "10:00",
"10:00" => "10:30",
"10:30" => "11:00",
...
"20:00" => "20:30",
"20:30" => "21:00"
]
There are some similar topics that show how to array time intervals but none of them have been able to show my specific issue.
Using DateTime class with DateInterval you can achieve what you need.
DateTime - https://www.php.net/manual/pt_BR/class.datetime.php
DateInterval - https://www.php.net/manual/pt_BR/class.dateinterval.php
<?php
$startTime = DateTime::createFromFormat("H:i", "09:00");
$endTime = DateTime::createFromFormat("H:i", "22:00");
$interval = DateInterval::createFromDateString("30 minutes");
$result = [];
while ($startTime <= $endTime) {
$result[$startTime->format('H:i')] = $startTime->add($interval)->format('H:i');
}
echo print_r($result, true);
You can add 30 min to by this command :
$time=date("H:i", strtotime('+30 minutes', $time));
For your problem can set a while loop(for check condition).
Then set and array and set index to :
date("H:i", strtotime('+'.$interval.' minutes', $time));`
And set value to :
date("H:i", strtotime('+'.(2*$interval).'minutes', $time));
Then update $time.
You should just be able to use a 'while' loop to increment a timestamp until $endtime is reached. Here's a working example:
$starttime = '09:00'; // Start time
$endtime = '21:00'; // End time
$interval = '30'; // In minutes
$result = [];
$last = strtotime($starttime);
$endtimestamp = strtotime($endtime);
while ($last < $endtimestamp) {
$next = strtotime("+{$interval} minutes", $last);
$result[date('H:i', $last)] = date('H:i', $next);
$last = $next;
}
var_dump($result);
Can be done using DateTime and DateInterval
<?php
date_default_timezone_set('UTC');
// define start/end
$begin = DateTime::createFromFormat('H:i', '09:00');
$end = DateTime::createFromFormat('H:i', '21:00');
// define the interval
$interval = new DateInterval('PT30M');
$interval->invert = 1;
// get date range
$daterange = new DatePeriod($begin, $interval, $end);
// loop and build your array
$range = [];
foreach ($daterange as $date){
$range[$date->format("H:i")] = $date->sub($interval)->format("H:i");
}
print_r($range);
Result:
Array
(
[09:00] => 09:30
[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
[16:00] => 16:30
[16:30] => 17:00
[17:00] => 17:30
[17:30] => 18:00
[18:00] => 18:30
[18:30] => 19:00
[19:00] => 19:30
[19:30] => 20:00
[20:00] => 20:30
[20:30] => 21:00
)
https://3v4l.org/FqViY

Fatal error: Maximum execution time of 120 seconds exceeded

Good day everyone, I'm trying . This is the code which demonstrates what I'm doing:
<?php
$NoOfGames = 10;
$time = array("06:00:00 " => "07:00:00", "07:00:00" => "08:00:00", "08:00:00" => "09:00:00", "09:00:00" => "10:00:00", "10:00:00" => "11:00:00", "11:00:00" => "12:00:00","12:00:00" => "13:00:00","13:00:00" => "14:00:00","14:00:00" => "15:00:00","15:00:00" => "16:00:00","16:00:00" => "17:00:00","17:00:00" => "18:00:00");
for($i=0;$i<$NoOfGames;$i+1){
$start_time = array_rand($time);
$end_time = $time[$start_time];
$time_new[$start_time] = $end_time;
}
$i =1;
foreach($time_new as $start => $end)
{
echo $i. ") ". $start . " to ". $end . "<br>";
$i++;
}
?>
However, this outputs
and I want to display it like this instead
1) 17:00:00 to 18:00:00
2) 06:00:00 to 07:00:00
3) 12:00:00 to 13:00:00
4) 11:00:00 to 12:00:00
5) 16:00:00 to 17:00:00
6) 08:00:00 to 09:00:00
7) 13:00:00 to 14:00:00
8) 15:00:00 to 16:00:00
9) 14:00:00 to 15:00:00
10) 09:00:00 to 10:00:00
This should work:
Change $i+1 to $i++
The reason is that $i+1 just evaluates to 1 every time (ie doesn't actually increment $i), meaning the loop never exits.

Repeat event on assigned day every week until specific date

When creating event I need to set event start and event end in datetime format (0000-00-00 00:00:00)
Then I have option to set weekly repeating of that event until specific date (0000-00-00)
But I can't insert repeating events properly in database. Here is what I have:
$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
$timestamp = strtotime($startDateTime);
$day_of_week = date('l', $timestamp);
$step = 1;
$unit = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd = new DateTime($repeatEndDate);
$repeatStart->modify($day_of_week);
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($repeatStart, $interval, $repeatEnd);
foreach ($period as $key => $date ) {
$repeatQuery = 'INSERT INTO event(start,end,status,repeats) VALUES ("'.$startDateTime.'","'.$endDateTime.'","'.$status.'","'.$repeatEndDate.'")';
$repeatResult = mysqli_query($db, $repeatQuery) or die (mysqli_error($db));
}
When I do print_r($date); it looks like this, no actual time just 00:00:00
DateTime Object
(
[date] => 2015-04-30 00:00:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
I know that I can't insert values like that but I don't know how to get correct values from objects.
So in this example I need to insert events that begin in 10:30:00 and end in 11:30:00(events always end same day) every Thursday starting at 2015-04-30 and ending at 2015-06-01. How can this be achieved?
Thank you
The problem is you're calling DateTime::modify with a day name (currently 'Thursday'). This value is calculated from the $startDateTime variable, and then used to modify that variable, so the only effect it has, is resetting the Time portion of that DateTime instance back to 0:00:00.
The following gives me the results I would expect:
(I've commented out the parts you need to remove from yours, to make it easier to see the difference)
date_default_timezone_set('Etc/UTC');
$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
#$timestamp = strtotime($startDateTime);
#$day_of_week = date('l', $timestamp);
$step = 1;
$unit = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd = new DateTime($repeatEndDate);
#$repeatStart->modify($day_of_week);
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($repeatStart, $interval, $repeatEnd);
foreach ($period as $key => $date ) {
echo($date->format('Y-m-d H:i:s')) . PHP_EOL;
}
The result from running the above is:
2015-04-30 10:30:00
2015-05-07 10:30:00
2015-05-14 10:30:00
2015-05-21 10:30:00
2015-05-28 10:30:00
When you modify your $repeatStart using the modify() method you are using the l format character which, according to the docs, returns
A full textual representation of the day of the week
by changing the $day_of_week format string to
$day_of_week = date('Y-m-d H:i:s', $timestamp);
I get the following output
DateTime Object
(
[date] => 2015-04-30 10:30:00
[timezone_type] => 3
[timezone] => Europe/London
)
DateTime Object
(
[date] => 2015-05-07 10:30:00
[timezone_type] => 3
[timezone] => Europe/London
)
DateTime Object
(
[date] => 2015-05-14 10:30:00
[timezone_type] => 3
[timezone] => Europe/London
)
DateTime Object
(
[date] => 2015-05-21 10:30:00
[timezone_type] => 3
[timezone] => Europe/London
)
DateTime Object
(
[date] => 2015-05-28 10:30:00
[timezone_type] => 3
[timezone] => Europe/London
)
Although, the modification is not actually necessary and the following code should achieve what you are looking for.
<?php
$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
$step = 1;
$unit = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd = new DateTime($repeatEndDate);
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($repeatStart, $interval, $repeatEnd);
foreach ($period as $key => $date ) {
$repeatQuery = 'INSERT INTO event(start,end,status,repeats) VALUES ("'.$startDateTime.'","'.$endDateTime.'","'.$status.'","'.$repeatEndDate.'")';
$repeatResult = mysqli_query($db, $repeatQuery) or die (mysqli_error($db));
print_r($date);
}

Split DateTime interval to single days

I have a PHP script which receives a set of Events from a database with begin/end DateTimes, which represent working times.
Begin | End
2013-08-14 10:00:00 | 2013-08-22 09:30:00
2013-08-08 07:00:00 | 2013-08-08 15:00:00
2013-08-09 07:00:00 | 2013-08-10 07:00:00
Now I want to calculate how much has been worked each single day. For the first row I would want an output like that:
Begin | End
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
....
2013-08-22 00:00:00 | 2013-08-22 09:30:00
I've seen some things with DatePeriod and DateInterval, but those didn't take time into account.
Thanks for your help.
DatePeriod and DateInterval DO take time into account, so you can use those classes.
Your data :
$intervals = [
['begin' => '2013-08-14 10:00:00', 'end' => '2013-08-22 09:30:00'],
['begin' => '2013-08-08 07:00:00', 'end' => '2013-08-08 15:00:00'],
['begin' => '2013-08-09 07:00:00', 'end' => '2013-08-10 07:00:00'],
];
Quick function I wrote :
function explodePeriodByDays($begin, $end) {
$days = [];
$dayInterval = new DateInterval('P1D');
$begin = new DateTime($begin);
$end = new DateTime($end);
$_end = clone $end;
$_end->modify('+1 day');
foreach ((new DatePeriod($begin, $dayInterval, $_end)) as $i => $period) {
$_begin = $period;
if ($i) $_begin->setTime(0, 0, 0);
if ($_begin > $end) break;
$_end = clone $_begin;
$_end->setTime(23, 59, 59);
if ($end < $_end) $_end = $end;
$days[] = [
'begin' => $_begin,
'end' => $_end,
];
}
return $days;
}
Example of function use :
foreach ($intervals as $interval) {
echo "Day intervals from {$interval['begin']} to {$interval['end']} : \n";
foreach (explodePeriodByDays($interval['begin'], $interval['end']) as $day) {
echo "\t {$day['begin']->format('Y-m-d H:i:s')} | {$day['end']->format('Y-m-d H:i:s')}\n";
}
echo "\n";
}
Output of the example :
Day intervals from 2013-08-14 10:00:00 to 2013-08-22 09:30:00 :
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
2013-08-17 00:00:00 | 2013-08-17 23:59:59
2013-08-18 00:00:00 | 2013-08-18 23:59:59
2013-08-19 00:00:00 | 2013-08-19 23:59:59
2013-08-20 00:00:00 | 2013-08-20 23:59:59
2013-08-21 00:00:00 | 2013-08-21 23:59:59
2013-08-22 00:00:00 | 2013-08-22 09:30:00
Day intervals from 2013-08-08 07:00:00 to 2013-08-08 15:00:00 :
2013-08-08 07:00:00 | 2013-08-08 15:00:00
Day intervals from 2013-08-09 07:00:00 to 2013-08-10 07:00:00 :
2013-08-09 07:00:00 | 2013-08-09 23:59:59
2013-08-10 00:00:00 | 2013-08-10 07:00:00
If I am understanding this correctly, you want to get the difference between a begin date and an end date.
I would go about converting the time with a strtotime, subtract the timestamps, then output the date.
$begin = strtotime("2013-08-14 10:00:00");
$end = strtotime("2013-08-14 23:59:59");
$difference = ($end - $begin);
echo gmdate("H:i:s", $difference);
This would give you the time in hours, minutes, then seconds.

Categories