Laravel carbon parse is giving incorrect output - laravel-5

I am working on chronic care health project in which i have to set future dates for patient injection. The doctor select start date for injection and then he select 3 days (i.e. Monday, Wednesday, Friday etc) which means patient will come 3 times a week.
The total injection are 20 that are divided by 3 times a week.
The problem that i am facing is that the parse function gives incorrect result if doctor select a day that matches today.
Lets suppose today is Wednesday and date is 2019-02-06 and doctor select 3 days that are:
Friday
Monday
Wednesday
Output should be:
- 2019-02-08
- 2019-02-11
- 2019-02-13
My code for controller:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $startDate->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $startDate->parse($perweek_visit3_day);
}
But when dump the 3 values it provide me :
2019-02-08
2019-02-11
2019-02-06 (incorrect)
For testing purpose I also parse the next visit date with each other like:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $perweek_visit1_dayDate ->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $perweek_visit2_dayDate ->parse($perweek_visit3_day);
}
but it again provided me the same output as :
- 2019-02-08
- 2019-02-11
- 2019-02-06
I don't know where i am going wrong, any support or help in this regard will be highly appreciated.
Thanks,

Related

How to get missing dates between 7 latest rows from database?

I want to get 7 latest rows (in order from down to up), for the current week (Sunday to Saturday), for the current logged in user.
To do this, I used this one method:
Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);
$strikes = UserStrike::where('user_id', $user)->whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->latest()->take(7)->get();
$strikes = $strikes->reverse(); //to order the list from DESC to ASC
But the problem with this method is that it doesn't get any missing days.
So if there are data like this for the current week (2020-05-12 is missing):
created_at: 2020-05-10
created_at: 2020-05-11
created_at: 2020-05-13
Then for that one day which is missing, there should be a null in array. Something like this:
$days = ["Sun", "Mon", null, "Wed"];
I'm having hard time to find a way to replace missing day with a null in array.
If anyone here can help me with this problem please leave your comment. Thank you in advance.
You can take data from DB and then create array by iterating on DateTimeInterval(). I don't know Laravel and Carbon well but it could be something like this:
Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);
$start = Carbon::now()->startOfWeek();
$end = Carbon::now()->endOfWeek();
$strikes = UserStrike::where('user_id', $user)
->whereBetween(
'created_at',
[$start, $end]
)
->latest()
->take(7)
->get();
$strikes = $strikes->reverse();
$timePeriod = new DatePeriod(
$start,
new DateInterval('P1D'),
$end
);
$days = [];
for ($i = 0; $i < count($timePeriod) $i++) {
$days[$i] = null;
foreach ($strikes as $strike) {
if ($strike->created_at->format('Y-m-d') == $timePeriod[$i]->format('Y-m-d') {
$days[$i] = $strike->created_at->format('Y-m-d');
break;
}
}
}

Calculating First Working Day adding holidays

I need to find the first working day of the month. Ideally it should be 01/MM/YYYY but the condition is that it should not be a Saturday or Sunday or a day from the List of Holidays.
List being:
ListOfHoliday
Name Day
Someday 01/01/2021
Someday 01/02/2021
Someday 31/03/2021
Someday 04/07/2021
Someday 25/12/2021
I was trying to use the below algo:
FWD = Text.concat(01, DateTime.CurrentMonthOfToday, DateTime.CurrentyearOfToday)
foreach(holiday in ListOfHoliday)
{
if (FWD == holiday)
{
DateTime.AddDays(ConvertTo.DateTime(FWD), 1)
if (ConvertTo.DateTime(FWD).DayOftheWeek == Saturday)
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
} else
{
if ((ConvertTo.DateTime(FWD).DayOftheWeek == Sunday))
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
}
}
}
}
But the result I get for the current month is 01/01/0001.
Here is the python code to find the first working day of a given month.
from datetime import datetime, timedelta
def working_day(temp,holid):
if temp.strftime('%A') == 'Saturday':
temp += timedelta(2)
return working_day(temp,holid)
elif temp.strftime('%A') == 'Sunday' or temp in holid:
temp += timedelta(1)
return working_day(temp,holid)
else:
return temp.strftime('%A')
date_format = "%d/%m/%Y"
holidays = ['01/01/2021','01/02/2021','31/03/2021','04/07/2021','25/12/2021']
holid = []
for day in holidays:
holid.append(datetime.strptime(day, date_format))
month = int(input('Enter the month')) # input should be 1,2,3....12
today ='01/'+str(month)+'/2021'
today = datetime.strptime(today, date_format)
print(working_day(today, holid))
output:
Enter the month 12
Wednesday

Writing a condition logics in kotlin in a better way in kotlin

I have a following condition logic and want to make it simple, is there a way to make it better without too much || && ? Some sample or example will be lovely! I would love to hear from you!
Logic that I want to achieve
From 0:00 on and 3 days before the birth month,
condition is the birth month is once or January 31st and
the present is run every December 29-31
val birthYear = child.birthYear!!.toInt()
val birthMonth = child.birthMonth!!.toInt()
val date = DateTime()
val year = date.year
val month = date.month
val day = date.day
val maxDayOfMonth = date.maxDayOfMonth
class DateTime {
private var mCalendar: Calendar = Calendar.getInstance().also { it.clear() }
val year: Int
get() = mCalendar.get(Calendar.YEAR)。。。。。
The following is the logic that I want to make it simpler
return date.year > child.birthYear!!.toInt() &&
((birthMonth == 1 && month == 12 || birthMonth - 1 == month)
&& day >= maxDayOfMonth - 2 || birthMonth == month)

How to obtain first date of each month based on given year range in Nifi flow

I would like to know what could be the best way to obtain the starting date values for each month based on the date range.
For example: If I am given a year range of 2015-11-10 and 2018-01-15(format YYYY-mm-dd). Then I would like to extract following dates:
2015-12-01
2016-01-01
.
.
2018-01-01
You can try to use this flow for generating the first day of each month in the provided date range.
Overall flow
Step 1 Configuration: Start
Step 2 Configuration: Configure Date Range
Provide the start and end dates as configuration parameters via this step.
Step 3 Configuration: Generate First Dates For Months
This uses a Groovy script, which is provided below
Groovy script
flowFile = session.get();
if(!flowFile)
return;
DATE_FORMAT = 'yyyy-MM-dd';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("start_date"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("end_date"));
allFirstDates = "";
Calendar calendar = Calendar.getInstance();
Set firstDaysOfMonths = new LinkedHashSet();
for (int i = 0; i <= endDate-startDate; i++) {
calendar.setTime(startDate.plus(i));
calendar.set(Calendar.DAY_OF_MONTH, 1);
firstDayOfMonth = calendar.getTime();
if (firstDayOfMonth.compareTo(startDate) >= 0) {
firstDaysOfMonths.add(calendar.getTime().format(DATE_FORMAT));
}
}
firstDaysOfMonths.each {
firstDayOfMonth -> allFirstDates = allFirstDates + firstDayOfMonth + "\n";
}
flowFile = session.putAttribute(flowFile,"all_first_dates", allFirstDates );
session.transfer(flowFile,REL_SUCCESS)
Step 4 Configuration: View Result
Output of run:
When the flow is run, the attribute all_first_dates will be populated with the first dates of each month in the date range.

How to use date helper in CodeIgniter to find time conflict?

I have a database table
TABLE subject_loads
id (int)
subject_name (varchar)
time_start (time)
time_end (time)
time_diff (decimal)
When I save it on database, it will first check whether the time is not conflicting from the other time already inputed. If it's okay, then compute the time_start and time_end to give me a difference between the two.
Example, 7:30 AM - 8:30 AM is already in database, when i input 8:00 AM - 9:00 AM it will say "conflicting with the other time". Only I can input before 7:30 AM or after 8:30 AM that doesn't overlap from 7:30 AM - 8:30 AM.
Can someone help on how to do this?
First you need to check if overlapping values (compared to new value) already exist. You will do that with:
$query = $this->db->get_where('subject_loads', array('time_start >= ' => $time_start, 'time_end <= ' => $time_end));
if((int)$query->num_rows() > 0)
{
//similar values exist
}
else
{
//you are free to insert values
}
Second part of issue:
$hm1 = "2:12 AM";
$hm2 = "4:41 PM";
$e = conv($hm2) - conv($hm1);
echo $e;
function conv($time)
{
$expl_time = explode(' ', $time);
$t = explode(":", $expl_time[0]);
if ($expl_time[1] == 'PM' || $expl_time[1] == 'pm')
{
$t[0] += 12;
}
return ($t[0] + round($t[1]/60, 1, PHP_ROUND_HALF_UP));
}

Resources