Yii2 - how to get a current quarter without the past quarter? - php

I've a problem here. I need to get a quarter of the year without the past quarter. F.e:
In my database I have a field created_at, which saves the timestamp of service created. I need to not involve those services, which was made in the past quarter. How should I do that?
I'm trying to write a SQL function like this to not involve those services, which was made in the past quarter:
$services= Service::find()
->where([
'client_service.created' => function ($model) {
return ceil(date('n') / 3) - 4;
But I guess I'm wrong. Thanks for any help.
Edited:
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
[client_service.created' => function ($model) {
return ceil(date('n') / 3) - 4;]

You are storing timestemp in db for service date , we can find current Quarter start date and end date from current month and use in query.
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime($current_year.'-01-01 00:00:00');
$end_date = strtotime($current_year.'-03-31 23:59:59');
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime($current_year.'-04-01 00:00:00');
$end_date = strtotime($current_year.'-06-30 23:59:59');
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime($current_year.'-07-01 00:00:00');
$end_date = strtotime($current_year.'-09-30 23:59:59');
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime($current_year.'-10-01 00:00:00');
$end_date = strtotime($current_year.'-12-31 23:59:59');
}
Use this $start_date and $end_date timestemp in Query as below :
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
['between', 'client_service.created', $start_date, $end_date]
])

Find start and end date of quarter
$date = new \DateTime(); // Current Date and Time
$quarter_start = clone($date);
// Find the offset of months
$months_offset = ($date->format('m') - 1) % 3;
// Modify quarter date
$quarter_start->modify(" - " . $months_offset . " month")->modify("first day of this month");
$quarter_end = clone($quarter_start);
$quarter_end->modify("+ 3 month");
$startDate = $quarter_start->format('Y-m-d');
$endDate = $quarter_end->format('Y-m-d');
Query
$services= Service::find()
->select(['client.id as client_id'])
->joinWith('client')
->where([
'service.type' => Service::TYPE,
'service.is_archived' => Service::ARCHIVED,])
->andWhere([
'or',
['client_service.status' => ClientService::STATUS_NEGATIVE],
['between', 'date_format(FROM_UNIXTIME(client_service.created), "%Y-%m-%d")', $startDate, $endDate]
You can also use mysql Quarter() to achieve the result.

Related

Carbon add hours issue

I'm struggling to understand why Carbon addHours() is not returning the proper time.
This is what i have in my controller :
public function create(Request $request)
{
$locationId = $request->input('location_id');
$beginningDate = $request->input('beginning_date');
$beginningTime = $request->input('beginning_time');
// $beginningDate = $request->input('beginning_date')->format('d-m-Y');
// $beginningTime = $request->input('beginning_time')->format('H:m');
$duration = $request->input('duration');
$totalPrice=PriceList::where('duration_to_hours', $duration)->value('price');
$reservationStartingDate = $beginningDate. ','.$beginningTime;
$calculateReservationDates = Carbon::parse($reservationStartingDate)->addHours($duration);
$endDate= $calculateReservationDates->format('d-m-Y');
$endTime = $calculateReservationDates->format('H:m');
$reservedCount = Reservation::where('beginning_date', '>=', $endDate)
->where('end_date', '>=', $beginningDate )
->where('beginning_time', '<=', $endTime)
->where('end_time', '>=', $beginningTime)
->count();
$totalBoxes = Box::where('location_id', $locationId)->count();
dd($endDate, $endTime);
$available = false;
if($reservedCount < $totalBoxes){
$available = true;
}
return view('checkout', compact('locationId', 'totalPrice', 'endDate', 'endTime', 'beginningDate', 'beginningTime', 'duration', 'available'));
}
But if for example the beginning time is 10:50 am and the duration is one hour, based on my logic it should return the $endTime to be 11.50 but it's only returning 11.04.
I do not get what I'm missing, do you have any idea?
Thank you
UPDATE
Here's the dd() of $reservationStartingDate :
^ "2022-04-22,00:53"
I think the H:m is wrong it's should be like this
$endTime = $calculateReservationDates->format('H:i');
m is refer to month, not minute. For minute, use i.
I hope it's helpful
// Y - year
// m - month - not minutes
// d - day
// H - hours in 24h format default UTC
// i - minutes
// s - seconds
$beginningDate = '2022-04-07';
$beginningTime = '10:30'; // 24h format UTC
// "2022-04-07 10:30" - you need this format
$reservationStartingDate = "$beginningDate $beginningTime";
$result = Carbon::createFromFormat('Y-m-d H:i', $reservationStartingDate)->addHours(1);
Result with an hour added
Carbon\Carbon #1649331000 {#1724
date: 2022-04-07 11:30:00.0 UTC (+00:00),
}

Date is exists in next coming months php

I am new to PHP. I am displaying the monthly events which is starting event from 03/31/2021 on every month, so here is a critical situation come because not every month exists 31 date. So there is some php function available to check this date is exists in next coming months. If exists then display the event on that date otherwise display the last date of the month.
$monthly_counter = 0;
$monthly_counter_offset = 0;
$begin = new DateTime($val->start); // $val->start = '03/31/2021'
$end = new DateTime($val->repeat_end_date); // $val->repeat_end_date= '03/31/2022'
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt)
{
$loop_date = $dt->format('m/d/Y');
$loop_date_formatted = $dt->format('Y-m-d');
$due_dates[] = $from_date_times;
$from_new_date_times = date('Y-m-d H:i', strtotime('+1 month', strtotime($from_date_times)));
$from_date_times = $from_new_date_times;
$deletedevents2=$val->deletedevents;
$arrayofdeleted=explode(",",$deletedevents2);
$originalDate = $from_date_times;
$newDateforcompare = date("m/d/Y", strtotime($originalDate));
if (in_array($loop_date_formatted, $arrayofdeleted))
{
continue;
}
$number_of_days = $val->number_of_days;
$end_new_date_times = date('Y-m-d H:i', strtotime('+'.$number_of_days.' day', strtotime($loop_date)));
if(date('Y-m-d ', strtotime($val->start))<date('Y-m-d', strtotime($val->ends)))
{
$end_new_date_times = date('Y-m-d H:i', strtotime('+1 day', strtotime($end_new_date_times)));
}
$skipcounter = $val->interval_value;
if (!empty($skipcounter) || $skipcounter != 1) {
$monthly_counter++;
$monthly_counter_offset++;
if ($monthly_counter == $skipcounter || $monthly_counter_offset == 1) {
$monthly_counter = 0;
} else {
continue;
}
} else {
}
$rows[] = array(
'id' => $val->id,
'title' => $val->title,
'description' => $val->calendar_comments,
'start' => $loop_date,
'end' => $end_new_date_times,
'borderColor'=>$val->color,
'backgroundColor'=>$val->color_bg,
'className'=>'timegridclass',
'allDay' => $allday,
);
}
Since you're using fullCalendar, you can simply specify an event which uses RRule to specify the recurrence, rather than using complex PHP code to try and generate it.
e.g.
events: [
{
title: "Sales Meeting",
rrule: "FREQ=MONTHLY;BYMONTHDAY=28,29,30,31;BYSETPOS=-1"
}
]
Working demo: https://codepen.io/ADyson82/pen/bGByBaV
This will generate an event which repeats on the last day of every month, regardless whether the month is 28, 29, 30 or 31 days long.
Obviously you can use PHP to generate this event object, and enhance it with a custom title, start/end dates etc as per your database contents. But I have shown you the basic approach.
Credit to this answer for the specific RRule string.
Documentation: https://fullcalendar.io/docs/rrule-plugin and https://github.com/jakubroztocil/rrule

List the salary coverage dates PHP

I want to generate a list of salary coverage dates using the last salary date of an employee. The employee gets a salary every 15 days. So every month the coverage should be 1st - 15th and 16th - last day of the month.
For example,
$last_salary_date = "2020-12-01";
$date_now = "2021-02-27";
// I should get the following start and end dates:
// 2020-12-01 - 2021-12-15
// 2020-12-16 - 2021-15-31
// 2021-01-01 - 2021-01-15
// 2021-01-16 - 2021-01-31
// 2021-02-01 - 2021-02-15
// 2021-02-16 - 2021-02-28
$last_salary_date = "2020-02-16";
$date_now = "2021-02-27";
// I should get the following start and end dates:
// 2021-02-16 - 2021-02-28
So far I've done something like this:
$start_date = new DateTime("2021-01-16");
$end_date = new DateTime(date("Y-m-d"));
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start_date, $interval, $end_date);
$salary_dates = [];
foreach ($period as $dt) {
if (date("Y-m-d") > $dt->format("Y-m-01")) {
$salary_dates[] = (object) [
'start_dt' => $dt->format("Y-m-01"),
'end_dt' => $dt->format("Y-m-15")
];
}
if (date("Y-m-d") > $dt->format("Y-m-15")) {
$salary_dates[] = (object) [
'start_dt' => $dt->format("Y-m-16"),
'end_dt' => $dt->format("Y-m-t")
];
}
}
return $salary_dates;
The problem is it still gets the 1-15th of the first month even though the start should be the 16th.I'm thinking of a better way to do this. Please help
Here is the modified implementation. It is sketched for understanding, but can also be written in a shorter form.
<?php
$start_date = new DateTime("2021-02-16");
$end_date = new DateTime("2021-02-27");
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start_date, $interval, $end_date);
$salary_dates = [];
foreach ($period as $dt) {
$check_date = function ($date) use ($start_date, $end_date) {
// check if salary interval is in correct range
return
$start_date->format("Y-m-d") <= $date->start_dt
and $end_date->format("Y-m-d") >= $date->start_dt; // are you sure it shouldn't be ->end_dt ? now it's on order
};
// check first two weeks in month
$salary_date = (object)[
'start_dt' => $dt->format("Y-m-01"),
'end_dt' => $dt->format("Y-m-15"),
];
if ($check_date($salary_date)) {
$salary_dates[] = $salary_date;
}
// check second two weeks in month
$salary_date = (object)[
'start_dt' => $dt->format("Y-m-16"),
'end_dt' => $dt->format("Y-m-t")
];
if ($check_date($salary_date)) {
$salary_dates[] = $salary_date;
}
}
print_r($salary_dates);

How to find recursive dates in php or mysql?

I have a data like this:
$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';
.........................
......................... // other data
$recursive = 1 means weekly and 2 means monthly.
Now what i am tring to do is if recursive type is weekly then add 7 days into it till 3 months and if recursive type is monthly then add 1 month into it till 3 months.
Exa:1 $date = '01-01-2014' and $recursive = '1'
Means in above example $recursive is weekly, so get a recursive date for January, February and March.
So the expected results are:
01-01-2014, 08-01-2014, 15-01-2014, 22-01-2014, 29-01-2014 (recursiive date in january)
05-02-2014, 12-02-2014, 19-02-2014, 26-02-2014 (recursiive date in february)
05-03-2014, 12-03-2014, 19-03-2014, 26-03-2014 (recursiive date in march)
Exa 2: $date = 15-04-2014 and $recursive = 1 then get recursive date for April, May and June.
output:
15-04-2014,22-04-2014,29-04-2014 (recursive date in april)
06-05-2014,13-05-2014,20-05-2014,27-05-2014 (recursive date in may)
03-06-2014,10-06-2014,17-06-2014,24-06-2014 (recursive date in june)
Exa 3 : $date = 01-01-2014 and $recursive = 2 then get recursive date for April, May and June.
This is monthly recursive, means add 1 month into it.
output:
01-01-2014
01-02-2014 (recursiive date in february)
01-03-2014 (recursiive date in march)
then i want to insert these dates with other data into database table.
so how to achive above things? should i write logic in php or use mysql query for it?
Thanks in advance.
SIDENOTE: currently i am using this accepted answer. but now i am trying to change that.
so basically what you want to do is something like this.
//you have to have a default time zone set.. so i just did this you should already have it in your .ini file
date_default_timezone_set('America/Los_Angeles');
$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';
//set your start date and end date
$startdate = date_create($date);
$enddate = date_create($date);
$enddate = date_add($enddate,date_interval_create_from_date_string("3 months"));
//set the interval string
if($recursive == '1'){
$str = "7 days";
} else {
$str = "1 month";
}
function recursivefunc($str, $start, $end){
//if the start is equal or bigger than end pop out.
$s = date_format($start,"Y/m/d");
$e = date_format($end,"Y/m/d");
if(strtotime($s) >= strtotime($e)){
return 1;
}
echo date_format($start,"Y/m/d"), '<br>'; //print out the starting date for each loop
$newDate = date_add($start,date_interval_create_from_date_string($str)); //increment the start
recursiveFunc($str, $newDate, $end); //call the function again
}
recursiveFunc($str, $startdate, $enddate); // initial call to the function
This is how i solve it.
$date = '01-01-2014';
$location = 'New Delhi';
$start = strtotime("+2 months", strtotime($date)); //start date
$end_date = date("Y-m-t", $start); // last day of end month
$end = strtotime($end_date); //last date
/* if recursion type is weekly */
if($json['recurrence'] == "1")
{
for($dd=$start; $dd<=$end;)
{
$new_date = date('Y-m-d', $dd);
$data[] = array(
'date' => $new_date,
'location' => $location
);
$dd = strtotime("+7 day", $dd); // add 7 days
}
}
if($json['recurrence'] == "2")
{
for($dd=$start; $dd<=$end;)
{
$new_date = date('Y-m-d', $dd);
$data[] = array(
'date' => $new_date,
'location' => $location
);
$dd = strtotime("+1 month", $dd); //add 1 month
}
}
echo "<pre>";
print_r($data);

Need help with looping through start-end date

I have an event calendar with start and end date like this:
16.08.2010 12:00:00 - 21.08.2010 20:00:00
16.08.2010 20:00:00 - 21.08.2010 23:00:00
18.08.2010 17:00:00 - 18.08.2010 19:00:00
Whenever an event goes on for more than one day, I need to loop through each day.
I found this thread, and I thought is could help me: How to find the dates between two specified date?
I can't use the solution for PHP 5.3, because my server run PHP 5.2.
The other solutions do not procude output.
This is what I try to do:
$events = $data['events'];
foreach($ev as $e) :
$startDate = date("Y-m-d",strtotime( $e->startTime ));
$endDate = date("Y-m-d",strtotime( $e->endTime ));
for($current = $startDate; $current <= $endDate; $current += 86400) {
echo '<div>'.$current.' - '.$endDate.' - '.$e->name.'</div>';
}
endforeach;
In theory, this should loop through all days for an event that extends over several days.
But that's not happening.
The logic is wrong somewhere.... please help :)
The problem is that you're trying to add numbers to a string. date('Y-m-d') produces a string like 2011-01-31. Adding numbers to it won't work [as expected]: '2011-01-31' + 86400 = ?.
Try something along these lines:
// setting to end of final day to avoid glitches in end times
$endDate = strtotime(date('Y-m-d 23:59:59', strtotime($e->endTime)));
$current = strtotime($e->startTime);
while ($current <= $endDate) {
printf('<div>%s - %s - %s</div>', date('Y-m-d', $current), date('Y-m-d', $endDate), $e->name);
$current = strtotime('+1 day', $current);
}
The date("Y-m-d") is wrong, you need the strtotime Result in your for loop. Try this, it should work:
$events = array(
array('16.08.2010 12:00:00', '21.08.2010 20:00:00', 'event1'),
array('16.08.2010 20:00:00', '21.08.2010 23:00:00', 'event2'),
array('18.08.2010 17:00:00', '18.08.2010 19:00:00', 'event3'),
);
$dayLength = 86400;
foreach($events as $e) :
$startDate = strtotime( $e[0] );
$endDate = strtotime( $e[1] );
if(($startDate+$dayLength)>=$endDate) continue;
for($current = $startDate; $current <= $endDate; $current += $dayLength) {
echo '<div>'.date('Y-m-d', $current).' - '.date('Y-m-d', $endDate).' - '.$e[2].'</div>';
}
endforeach;

Categories