Today is 9/25/2019
I'm trying to query my visitor with in this year of 2019 only.
Then, I want to know how many are on
1,2,3,4,5,6,7,8,9
I've tried
$raw = Visitor::query()
->whereYear('created_at', now()->year -1)
->get()
->pluck('created_at');
$data = [];
foreach ($raw as $i=>$date) {
$data[$i] = Carbon::parse($date)->format('m');
if( Carbon::parse($date)->format('m')[0] != 0 ){
$data[$i] = Carbon::parse($date)->format('m');
}else{
$data[$i] = str_replace('0','',Carbon::parse($date)->format('m'));
}
}
// dd($data);
$dataValues = array_count_values($data);
dd($dataValues);
I got
array:5 [▼
8 => 314
9 => 916
10 => 764
11 => 827
12 => 765
]
Why would I get anything in the future since this month is only September (9) ?
How can I correct it?
I was hoping to get something like this
array:9 [▼
0 => 314
1 => 916
2 => 764
3 => 827
4 => 165
5 => 225
6 => 565
7 => 65
8 => 1265
]
I would like to get an array-like above output.
There are a couple of things you might try.
Laravel automatically makes created_at into a Carbon object, so if you create your query like this:
$raw = Visitor
::whereYear('created_at', \Carbon::now()->year) <<-- Note 2019, not -1 for 2018
->select('created_at')
->get();
Here you get an object with the Carbon date field, created_at. You then won't need to use parse in your if-checks.
I suspect perhaps the answer to your question, though, is that it looks like you are pulling items from 2018, when you want 2019. You have subtracted a year off of this year in your original query.
you should extract month from date then groubby ,and don't forgot select count :
Visitor::whereYear('created_at', Carbon::now()->year)
->select(DB::raw("MONTH(created_at) month"),DB::raw("count('month') as vistors_count"))
->groupby('month')
->get();
Related
I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856
You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online
In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();
try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);
This question already has answers here:
How to store values from foreach loop into an array?
(9 answers)
Closed 3 years ago.
I'm trying to create a multidimensional array in Laravel 5.8 but I can't seem to get it to work.
It's running on PHP 7.3 and Laravel 5.8.
Here's what I've already try:
foreach ($locations as $location) {
$email_data = array($address => array($location));
}
$arr = array($email_data[count($email_data)-1] => $address);
for ($i=count($email_data) - 2 ; $i>=0 ; $i--) {
$arr = array($email_data[$i] => $arr);
}
foreach ($ageMatches as $ageMatch) {
$age['Age'] = $ageMatch[1];
array_push($arr, $age);
}
Here's the data I'm using:
ELY FARM
THE ADDRESS
BW8010 2.55 UNIT 1
Logged 03 JUN 19 23:59
SUMMARY REPORT
----UNIT 1 PERCH 1-----
House 1, Pen 1
Age DAYS 7
Total 3997
Average GMS 64
Deviation GMS 14
Evenness % 29
C.V. % 21.9
Daily gain GMS 9
----UNIT 1 PERCH 2-----
House 1, Pen 2
Age DAYS 7
Total 3849
Average GMS 73
Deviation GMS 17
Evenness % 29
C.V. % 23.3
Daily gain GMS 9
So I want to to be like:
array(
"Ely Farm" => array(
"Unit 1 Perch 1" => array(
"Age" => 7,
"Total" => 3849,
),
"Unit 1 Perch 2" => array(
"Age" => 7,
)
),
"Next Location" => array(
array(
),
);
At the moment I have the following and I can't work where I'm going wrong (if I dump $location[1] it does come out with the location instead of hyphens but when I don't dump it comes out as below).
array:1 [▼
"Ely Farm" => array:1 [▼
0 => array:2 [▼
0 => "------------------------"
1 => "---------------"
]
]
]
In this block you are rewriting $email_data each iteration
foreach ($locations as $location) {
$email_data = array($address => array($location));
}
What you should do instead is
foreach ($locations as $location) {
$email_data[] = array($address => array($location));
}
This will continuously add the values instead of replacing them. You have the same issue below too with $arr
How is it possible that whereNotIn() plus whereIn() doesn't equal total count?
Running this:
$updatedBreeds = [
86,
113,
// etc ....
];
DB::enableQueryLog();
dump(Breed::count());
dump(Breed::whereIn('id', $updatedBreeds)->count());
dump(Breed::whereNotIn('id', $updatedBreeds)->count());
dd(DB::getQueryLog());
Returns this:
159
39
0
Am I missing something here? The whereNotIn() call should return 120 results.
Apparently, one of the values in the array was null. Which strangely enough led to this behavior.
Here is a dump on $updatedBreeds:
[
0 => 86
1 => 113
- 2 => null // When I removed this value, the whereNotIn() worked
2 => 44
3 => 8
4 => 54
5 => 54
// ...
]
array:23 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 8
6 => 9
7 => 10
8 => 11
9 => 12
10 => 15
11 => 16
12 => 17
13 => 18
14 => 19
15 => 22
16 => 23
17 => 24
18 => 25
19 => 26
20 => 29
21 => 30
22 => 31
]
this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe
->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data
There is no whereDayIn() or similar method, but you can do this:
->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
foreach ($workdays as $day) {
$q->whereDay('Clock_Day', '=', $day, 'or');
}
})
If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:
->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)
You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.
Assume that selected date from Canlender is 02/09/2011. To store weekly date into array from 20/09/2011 is
for($i=0; $i<7; $i++)
{
$WeeklyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
My question is how to store monthly date into array from the selected date.
Many thanks
---Update----------
The final result of monthlyDate should look like the following:
$monthlyDate= array{2011-08-03, 2011-08-04, 2011-08-05, 2011-08-06, 2011-08-07 ....2011-08-31, 2011-09-01, 2011-09-02}
First, calculate the number of days in a month using cal_days_in_month and then proceed as you are doing with weeks eg:
$days = cal_days_in_month(CAL_GREGORIAN, 9, 2011);
for($i = 0; $i <= $days; $i++)
{
$MonthlyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
Notice that CAL_GREGORIAN is a built-in constant.
Working Example
Whenever programs are incrementing a date using 86400 there is a risk of unexpected output because of DST.
By using strtotime() with a unit larger than hours (like days, weeks, months, etc.) preventing any DST hiccups. Note: a DateTime object approach can be used but for this case, it is unnecessary overhead.
The following is an adjusted form of a one-liner date range function I developed.
Here is the online demo for this case.
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
$date='2011-09-02';
$monthlyDate=getDatesFromRange(date("Y-m-d",strtotime("$date -1 month +1 day")),$date);
var_export($monthlyDate);
output as desired/expected:
array (
0 => '2011-08-03',
1 => '2011-08-04',
2 => '2011-08-05',
3 => '2011-08-06',
4 => '2011-08-07',
5 => '2011-08-08',
6 => '2011-08-09',
7 => '2011-08-10',
8 => '2011-08-11',
9 => '2011-08-12',
10 => '2011-08-13',
11 => '2011-08-14',
12 => '2011-08-15',
13 => '2011-08-16',
14 => '2011-08-17',
15 => '2011-08-18',
16 => '2011-08-19',
17 => '2011-08-20',
18 => '2011-08-21',
19 => '2011-08-22',
20 => '2011-08-23',
21 => '2011-08-24',
22 => '2011-08-25',
23 => '2011-08-26',
24 => '2011-08-27',
25 => '2011-08-28',
26 => '2011-08-29',
27 => '2011-08-30',
28 => '2011-08-31',
29 => '2011-09-01',
30 => '2011-09-02',
)