Display total days between two dates if status is completed - laravel

I am trying to count the number of days between two dates the carbon::now and $start_date and when the status == complete the counting of days stops then get the total number of days.
if ($this->status === 'COMPLETED') {
$now = Carbon::now();
$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
$this->start_date_to_current_date = $start_date->diffInDays($now, true);
}
But the problem is, the days still continues to count even the status is completed like for example the total days is 3 then in the next day it becomes 4 then ive tried doing this:
if ($this->status == null && $this->status === 'COMPLETED') {
$now = Carbon::now();
$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
$this->start_date_to_current_date = $start_date->diffInDays($now, true);
}
But the value becomes zero. why is that ? :/

You need to log the complete date somewhere, and use it instead of now().
For instance:
if ($this->status === 'COMPLETED') {
$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
$start_date->diffInDays($this->completed_date); // completed_date should come from db.
}
The problem here is that you're using the current date as complete date which is obviously changing everyday, hence you get different result each day.

let it only do that when you not yet, and do not repeat do it.
`if ($this->status == null && $this->status !== 'COMPLETED') {`
`$now = Carbon::now();`
`$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);`
`$this->start_date_to_current_date = $start_date->diffInDays($now, true);`
}

Related

Carbon Get diffInDays if less than 1 month or get diffInMonths if more than 1 month

I want to show days if the range between date1 and date2 is less than 1 month, and if it was greater than 1 month, I want to get the date difference in months.
I have a class and function like code bellow:
<?php
namespace App\Utility;
use Carbon\Carbon;
class DaysBetweenTwoDate
{
public function count($start_date, $end_date)
{
$date1 = Carbon::parse($start_date);
$date2 = Carbon::parse($end_date);
if ('less than 1 month') {
$result = $date1->diffInDays($date2);
} else if ('more than 1 month') {
$result = $date1->diffInMonths($date2);
}
return $result;
}
}
I know that if I want to get the time difference in days I can use diffInDays, and if I want to get the time difference in months I can use diffInMonths. But what I don't know is how to check if the date range between date1 and date2 is greater or less than 1 month?
What I need to do is simply check result from diffInMonths is greater than 1 month or less than 1 month. And if it was less than 1 month, I just need to use diffInDays to calculate the time difference in days.
<?php
namespace App\Utility;
use Carbon\Carbon;
class DaysBetweenTwoDate
{
public function count($start_date, $end_date)
{
$date1 = Carbon::parse($start_date);
$date2 = Carbon::parse($end_date);
$result = $date1->diffInMonths($date2);
if ($result == 0) {
$result = $date1->diffInDays($date2);
}
return $result;
}
}
You can use the condition as you mentioned in your question, like following
$result <= 0
While using diffInMonths, we should get the difference between 2 dates. But the month of "February" has only 28 or 29 days, which will leads a wrong difference between months. So we need to use exact floating difference to get the exact difference in month by using floatDiffInMonths method and then need to round the value and convert it into integer as the following code.
<?php
namespace App\Utility;
use Carbon\Carbon;
class DaysBetweenTwoDate
{
public function count($start_date, $end_date)
{
$date1 = Carbon::parse($start_date);
$date2 = Carbon::parse($end_date);
$result = (int)round($date1->floatDiffInMonths($date2))
if ($result <= 0) {
$result = $date1->diffInDays($date2);
}
return $result;
}
}
If in the above case the difference of the date1 & date2 is greater than 1 month it will returns the month.
If the result of floatDiffInMonths is less then or equals to 0 will checks with diffInDays and returns the days between 2 dates.

how to increment day by one to find the next desired day in laravel

I've an array of day name like ['Wednesday','Sunday','Monday']
now I want to find the next available dates That matched with the array from today's date. I want date. I tried for so long but none of them were successful. My code is given below
$datesAvailable = array();
$count = 0;
$dateToday = Carbon::now()->toDateTimeString();
//$avlDays is the array of day names
$avlDays = DB::table('doctor_schedules')
->select('available_days')
->where('department',$receivedDepartment)
->Where('doctor_id', $receivedDoctor)
->get();
for($k=5; $k > 0; $k++)
{
$dateToday = $dateToday->endOfWeek();
// $parsedDate = Carbon::parse($dateToday);
$dateTodayFormated = new Carbon($dateToday);
$nextDayName = $dateTodayFormated->englishDayOfWeek;
for($i = 0; $i <= 2; $i++)
{
if($avlDays === $nextDayName)
{
$datesAvailable[$count] = $nextDayName;
$count++;
}
}
}
return (['availableDates' => $dateToday]);
Solved
For the current date use
now()
https://laravel.com/docs/6.x/helpers#method-now
Carbon does this.
foreach ($avlDays as $day) {
$nextDay = Carbon::parse("next $day");
// do something with $nextDay...
}
So, assuming $avlDays is returning you a Carbon date range:
$avlDays = DB::table('doctor_schedules')
->select('available_days')
->where('department',$receivedDepartment)
->Where('doctor_id', $receivedDoctor)
->get()
->toArray();
The code below assumes that the array value from ['monday', 'tuesday' ...] is saved as $weekday.
If you are trying to find a specific date within a range that falls on a certain weekday, you can do something like:
$availableAppointments = [];
foreach ($avlDays as $day) {
if ($day->isDayOfWeek($weekday)) {
$availableAppointments[] = $day;
}
Then you can use $availableAppointments to list out the available dates on that day of the week.

comparing two pair of date using Carbon - Laravel

a user chose date and I get that date and make 3 more dates as below
$start_date = Carbon::parse($request->start_date);
$m1 = $start_date->addMonths(1);
$m3 = $start_date->addMonths(3);
$m6 = $start_date->addMonths(6);
then I get the database values which I need to compare the dates with as below
$model = Model::WhereDate('start_date','>',$request->start_date)->get();
and I loop through them
$duration = 0;
foreach ($model as $value) {
if (Carbon::parse($value->start_date)->between($start_date, $m3) || Carbon::parse($value->end_date)->between($start_date, $m3)) {
$duration = 1;
}
if (Carbon::parse($value->start_date)->between($start_date, $m3) || Carbon::parse($value->end_date)->between($start_date, $m3)) {
$duration = 3;
}
if (Carbon::parse($value->start_date)->between($start_date, $m6) || Carbon::parse($value->end_date)->between($start_date, $m6)) {
$duration = 6;
}
}
is there a better way to do this? I'm not OK with comparing like this Carbon::parse($value->start_date)->between($start_date, $m6) || Carbon::parse($value->end_date)->between($start_date, $m6)
PS - this not to get the duration between dates but to compare them in given date range whether it's in that range.the twist is that comparing date is also a range dates.
I apologies for my language if there is any unclear part; let me know I'll explain it

Laravel carbon retrieve record even when it shouldn't

I am searching by start and end date or with a weekday, which kind of works fine however if weekday is equal to 'WE' and today's weekday is equal to 'WE' it still brings back results even when $today date is < than endate.
public function show($id)
{
$weekMap = [
0 => 'SU',
1 => 'MO',
2 => 'TU',
3 => 'WE',
4 => 'TH',
5 => 'FR',
6 => 'SA',
];
$todayWeek = Carbon::now()->dayOfWeek;
$today= Carbon::now();
$weekday = $weekMap[$todayWeek];
$event = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'))
->orWhere('weekday', '=', $weekday)
->get();
dd($event);
return view('events.showEvent', compact('event'));
}
If I change orWhere to where, results are all good, however user not always enters a weekday so it can be empty. I want it to work like this:
output everything where startdate < today and where endate > today and if weekday is not empty, output everything where startdate < today, endate > today and weekday = $weekday. I hope that makes sense.
//edit
sample data:
data
So what I want is really search by frequency so it would look like this:
if frequency = daily
compare start and end with today's date.
if frequency = weekly
compare start and end with today's date and weekday.
bring back all results for frequency = daily+weekly
you can use if condition in query like this:
$query = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'));
if ($weekday != '') {
$query->where('weekday', $weekday);
}
$event = $query->get();
also
condition if ($weekday != '') check $weekday isset or not you can use
if ( ! is_null($weekday )) instead

LARAVEL, data increment

I would like to make an auto increment with a date + a number. Example: 16022017-1. However I can not add the date + the dash + the number.
1- I retrieve the latest invoices in the database
$exist = Contrats::where('number','like','%'.$dateNow->format('dmY').'%')->orderBy('number', 'desc')->get();
2- Here my condition adds value after the date however I can not add the "-" and the number.
if (count($exist) == 0){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY');
} elseif (count($exist) == 1){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY'), '-', 1;
} else {
echo "pb";
}
Do you have an idea how I can increment my date, dash and number? Thank you for your answers.
Quick shoot from the hip answer as i am munching on my salad... Seems that your suffix (number after the date) is the actual count since you seem to be skipping the first instance (0). Eloquent returns a collection... use the ->count() ...
if ($exist->count() > 0){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY') . '-' . $exists->count();
} else {
$date = new \DateTime(null);
$contrat->number = $date->format('dmY');
}

Resources