How to +1 day from retrive endDate data in laravel controller? - php

I want to get enddate by + 1 day
$data=[
"title" =>$event->event_name,
"start" =>$event->start_date,
"end" =>new \DateTime($event->end_date.'1 day'),
"textColor" =>"white",
"backgroundColor" =>"black",
];
but the code make the fullcalendar not show any data in the view
how to make it works + 1 day?
thanks before

Try this code it's work.
<?php
$data=[
"title" => $event->event_name,
"start" => $event->start_date,
"end" => date('Y-m-d H:i:s', strtotime($event->end_date . ' +1 day')),
"textColor" => "white",
"backgroundColor" => "black",
];
?>

Related

How to get time slots for a specific date

Hope all are doing well. I need to print an array as time slots.
Assume that there are 2 orders for 2021.11.15 on 11:30am to 12:00pm and 2:00pm to 4:15pm.
My order needs 1h 30m to complete. Therefore time slots should be in between 8:00am and 6:00pm skipping those times for already exist orders.
My expected results should be:
array:2 [
0 => array:2 [
"start" => "08:00:00"
"end" => "9:30:00"
]
1 => array:2 [
"start" => "09:30:00"
"end" => "11:00:00"
]
2 => array:2 [
"start" => "12:00:00"
"end" => "13:30:00"
]
3 => array:2 [
"start" => "16:15:00"
"end" => "17:45:00"
]
]
Following line is used to get exist orders object with their start and end times.
$existOrders = $this->orderHasPropartnerService->getOrderExistForDateProPartner($proPartnerDefaultLocation->id, $selectedDateRecord->date);
Then I just looped it.
if ($existOrders->count() > 0) {
$dateStartTime = $selectedDateRecord->time_from;
$x = 0;
$firstEndingTime = Carbon::parse($dateStartTime)->addMinutes($totalTimeToOrder)->format('H:i:s');
foreach ($existOrders as $key1 => $existOrder1) {
if ($existOrder1->order->time_slot_from < $firstEndingTime && $existOrder1->order->time_slot_to >= $firstEndingTime) {
$timeCheckArray[$x]['start'] = $existOrder1->order->time_slot_to;
$timeCheckArray[$x]['end'] = Carbon::parse($existOrder1->order->time_slot_to)->addMinutes($totalTimeToOrder)->format('H:i:s');
} else {
$timeSlotArray[$x]['start'] = $dateStartTime;
$timeSlotArray[$x]['end'] = $firstEndingTime;
$timeCheckArray[$x]['start'] = $firstEndingTime;
$timeCheckArray[$x]['end'] = Carbon::parse($firstEndingTime)->addMinutes($totalTimeToOrder)->format('H:i:s');
}
if (isset($existOrders[$key1+1])) {
if ($existOrders[$key1+1]->order->time_slot_from < $timeCheckArray[$x]['end'] && $existOrders[$key1+1]->order->time_slot_to >= $timeCheckArray[$x]['end']) {
} else {
$timeSlotArray[$x+1]['start'] = $timeCheckArray[$x]['start'];
$timeSlotArray[$x+1]['end'] = $timeCheckArray[$x]['end'];
}
}
}
}
As for the above example $dateStartTime will be 8:00am. Value of $totalTimeToOrder will be 1h 30m.
When I try to print $timeSlotArray it'll result as follows:
array:2 [
0 => array:2 [
"start" => "08:00:00"
"end" => "09:30:00"
]
1 => array:2 [
"start" => "08:00:00"
"end" => "09:30:00"
]
]
It is really appreciated if someone point me out where I did mistakes in this logic. Thank you so much guys for your valuable time for a problem of mine.
The best approach would be to run a for loop to check that the New order does not fall between the booked times .
Try something similar to this by converting it to a PHP Date object
$NewOrder= "4:59 pm";
$start= "5:42 am";
$end= "6:26 pm";
$date1 = DateTime::createFromFormat('h:i a', $NewOrder);
$date2 = DateTime::createFromFormat('h:i a', $start);
$date3 = DateTime::createFromFormat('h:i a', $end);
if ($date1 > $date2 && $date1 < $date3)
{
echo 'Not safe to add it ';
}

Get Start and end date of Week according to string

I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )

MongoDB using UTCDateTime in $gte doesn't work

I'm writing a query to search for users created at specific date e.g.
$date = '2018-05-02 15:46:41.000Z';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date))]
]
]);
});
Given an sample data like
{
"_id" : ObjectId("5ae9dd61ef7a5754f159c656"),
"name" : "Effie Larkin",
"created_at" : ISODate("2018-05-02 15:46:41.000Z"),
}...
Expected result should be
{
"users" : [
"_id" : {"$oid": "5ae9dd61ef7a5754f159c656"},
"name" "Effie Larkin",
"created_at" : "2018-05-02 15:46:41"
]
}...
But I get an empty json array []. I think the problem is in UTCDateTime function but I don't how to pass dateTime to it. I want to pass a time interval like 1 week ago or yesterday etc... if anyone can help me will be much appreciated, thanks
Update, the example below fixed it
$date = '2018-05-02';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date)*1000)]
]
]);
});

How to override an array of overlaping timeslots

So I need to create a sort of day-per-day calendar with available times, in order for a user to be able to book a meeting with one doctor from a cabinet of multiple doctors.
I hope that this explanation is not too weird already..
Btw I use Laravel 5.5
Here's an example:
Default Schedule of the cabinet : 9:00 to 19:00
Doctor 1 says that on monday, he'll be only available from 13:00 to 15:00
Doctor 2 says that on monday, he'll be only available from 10:00 to 14:00
When I query the available timeslots :
$ids = Doctor::all()->pluck('id');
$workingSchedules = WorkingSchedule::whereIn('user_id', $ids)
->orderBy('start_date')
->whereDate('start_date', '=', $this->datetime->format('Y-m-d'))
->get();
I get:
0 => [
"start_date" => "2017-09-18 10:00:00"
"end_date" => "2017-09-18 14:00:00"
]
1 => [
"start_date" => "2017-09-18 13:00:00"
"end_date" => "2017-09-18 15:00:00"
]
And if nothing shows up from the Database then I use the default cabinet hours.
Then I use Carbon diffInMinutes() method to construct an array of 30 minutes timeslots between those date range (that the user can select).
Anyway, for my script to work correcty I need to transform the result I showed you into this:
0 => [
"start_date" => "2017-09-18 10:00:00"
"end_date" => "2017-09-18 15:00:00"
]
As I only have two timeslots in this example it might be simple a solution, but I might also get an array of 10 timeslots that overlapse one another..
Can somebody help me find a elegant solution that will cover all possible case ?
Thanks a lot in advance.
To be easier, I will suppose $workingSchedules is an array of numbers, then we can easily compare elements
$workingSchedules = [
[
'start_date' => 1,
'end_date' => 5,
],
[
'start_date' => 13,
'end_date' => 16,
],
[
'start_date' => 16,
'end_date' => 17,
],
];
$result = [$workingSchedules[0]];
$index = 0;
foreach ($workingSchedules as $row) {
if ($result[$index]['end_date'] >= $row['start_date']) {
$result[$index]['end_date'] = max($result[$index]['end_date'], $row['end_date']);
} else {
$index++;
$result[] = $row;
}
}
var_dump($result);
Above code will print:
[
[
'start_date' => 1,
'end_date' => 5,
],
[
'start_date' => 13,
'end_date' => 17,
],
]
You can custom the code to compare 2 dates instead numbers
If $workingSchedules is empty, we can simply return default schedule
To merge overlapping time-periods, you could use this code:
$result = [];
$i = -1;
foreach ($workingSchedules as $row) {
if ($i < 0 || $row["end_date"] > $result[$i]["end_date"]) {
if ($i >= 0 && $row["start_date"] <= $result[$i]["end_date"]) {
$result[$i]["end_date"] = $row["end_date"];
} else {
$result[++$i] = $row;
}
}
}
$result will then have non-overlapping periods only.
I hope this will help.
$workingSchedules=array(
0=>array(
"start_date" => "2017-09-18 10:00:00",
"end_date" => "2017-09-18 14:00:00"),
1=>array(
"start_date" => "2017-09-18 13:00:00",
"end_date" => "2017-09-18 15:00:00"
)
);
foreach ($workingSchedules as $schedule){
$start=new DateTime($schedule['start_date']);
$end=new DateTime($schedule['end_date']);
while ($start<=$end){
echo $start->format('Y-m-d H:i')."<br/>";
$start=$start->add(new DateInterval('PT'.'30'.'M'));
}
}

Limit form entries on a per hour basis?

I'm currently using the Gravity Forms plugin and it's set up to limit form entries per day, week, month and year; unfortunately, I need it by hour. Here is the original code that I'm editing:
private static function get_limit_period_dates($period){
if(empty($period))
return array("start_date" => null, "end_date" => null);
switch($period){
case "day" :
return array(
"start_date" => gmdate("Y-m-d"),
"end_date" => gmdate("Y-m-d 23:59:59"));
break;
case "week" :
return array(
"start_date" => gmdate("Y-m-d", strtotime("Monday this week")),
"end_date" => gmdate("Y-m-d 23:59:59", strtotime("next Sunday")));
break;
case "month" :
$month_start = gmdate("Y-m-1");
return array(
"start_date" => $month_start,
"end_date" => gmdate("Y-m-d H:i:s", strtotime("{$month_start} +1 month -1 second")));
break;
case "year" :
return array(
"start_date" => gmdate("Y-1-1"),
"end_date" => gmdate("Y-12-31 23:59:59"));
break;
}
}
I've attempted the following code, I figured starting at 0 minutes and 0 seconds, then adding an hour would do the trick, but it isn't working:
case "hour" :
return array(
"start_date" => gmdate("H:00:00"),
"end_date" => gmdate("H:i:s", strtotime("+1 hour")));
break;
I've never used the gmdate function, so I'm eager to get a grasp on this if someone's willing to briefly explain what I'm missing. Thanks!
I think this might be what you want:
case "hour" :
$hour_start = gmdate("Y-m-d H:00:00");
return array(
"start_date" => $hour_start,
"end_date" => gmdate("Y-m-d H:i:s", strtotime("{$hour_start} +1 hour")));
break;
You have to do 1 hour after the start hour. Otherwise it takes it as 1 hour from now.

Categories