i got a problem in my project i am using Laravel 4.2.
Piece of code which have problem..
$xx = xx::where('user_id','=',$user_id)
->where('date','>=',$first_day) //$first_day is first day of any month
->where('date','<=',$last_day)//$last_day is last day of same month
->get();
print_r($xx);die();
//records present but empty array for current month but working fine for any previous month
Result
Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )
Laravel last query log
Array ( [query] => select * from x_xx where user_id = ? and date
= ? and date <= ? [bindings] => Array ( [0] => 17 [1] => 2016-04-1 [2] => 2016-04-30 ) [time] => 0.69 )
Running in mysql phpmyadmin i got 15 records
select * from x_xx where user_id = 17 and date >= '2016-04-1'
and date <= '2016-04-30' //15 total records found in mysql
Related
I have a database that has budgets in them (for this, I have modified the budgets to be fake).
Long story short, I am attempting to run the following SQL query on the database, and then display a report based on it.
Here is the query: SELECT MONTHNAME(date) AS month, product, SUM(amount) AS spend FROM client_budgets WHERE advertiser_id = '$advertiser_id' GROUP BY MONTHNAME(date), product ORDER BY MONTH(date) ASC
I then clean the data a little, here is the code for that:
foreach($data AS $key => $val) {
$clean_data[] = $val;
}
From this, I get the following (this is a PHP array of the data. I have cut it off since it has about 100K rows in the database currently).
Array
(
[0] => Array
(
[month] => January
[product] => Internet
[spend] => 12000.00
)
[1] => Array
(
[month] => January
[product] => Radio
[spend] => 12250.00
)
[2] => Array
(
[month] => February
[product] => Billboards
[spend] => 6000.00
)
[3] => Array
(
[month] => February
[product] => Internet
[spend] => 16000.00
)
)
My goal is to end up being able to display in a CSV (I already have the headers set for the CSV), the following:
Month, Internet, Radio, Billboards, Television
January, 12000, 12250, 6000, 0
February, 16000, 7000, 6000, 2000
....
I am stuck right now trying to sort the data correctly, and if there is no data for Television for instance one month, to display a 0. Any help would be appreciated! I have been attempting this for almost 3 days now.
I have a php mysql query below that works great when all rows within the current day (-13-) in a date (05-13-2014):
$h = $time->igetDay();
$h = "%{$h}%";
$result = query("SELECT * FROM table_name WHERE table_column LIKE '%s'", $h);
print json_encode($result);
What I am trying to do now is get all the rows within a current week range.
I created a function that returns an array of the 7 upcoming days as shown below:
$h = $time->igetWeekRange();
print_r($h);
//this displays: Array ( [0] => 13 [1] => 14 [2] => 15 [3] => 16 [4] => 17 [5] => 18
[6] => 19 [7] => 20 )
//I can also display the array like this:
Array ( [0] => -13- [1] => -14- [2] => -15- [3] => -16- [4] => -17- [5] => -18-
[6] => -19- [7] => -20- )
My problem is that I don't know how I would use the query:
$result = query("SELECT * FROM table_name WHERE table_column LIKE '%s'", $h);
In order to display all the rows that have the seven values of the array above.
What this query should do is display all the rows of the table that have a date within the next seven days of the current day. How can I do this? Let me know if there is anything else I could provide to help you understand better what I am trying to do.
Thanks in advance, any help would be greatly appreciated.
Try below:
$h = $time->igetWeekRange();
foreach ($h as $val) {
$query_parts[] = "'%".mysql_real_escape_string($val)."%'";
}
$string = implode(' OR table_column LIKE ', $query_parts);
$result = query("SELECT * FROM table_name WHERE table_column LIKE {$string}");
print json_encode($result);
When working with things like this I store my time in epoch so it is as simple as doing a little math to get whatever you want.
<?php
$the_date =new DateTime("2014-05-13 10:46:09"); //Or new DateTime() for current date
$date_in_epoch_time= $the_date->format('U');
$a_week_ago=$date_in_epoch - (7 * 24 * 60 * 60)); //subtract a week in seconds
?>
ASSUMING That you'll convert the time into epoch time before inserting it into the database, you now have the date in seconds so if you want items in a database that are from a week previous you can do:
"SELECT * FROM table_name WHERE other_table_column >= $a_week_ago AND table_column LIKE '%s', $h";
How to count/group data from array into hours from MySQL timedate format or unix timestamp?
My array:
Array (
[0] => Array (
[0] => id
[1] => name
[2] => 2013-01-10 00:36:00
)
[1] => Array (
[0] => id
[1] => name
[2] => 2013-01-10 00:36:00
)
)
How to shrink thousands of items like this and agregate them somehow to hours or even days? I just need rough number of rows with given hour in specyfic day.
Valid code for my case from answer from #Robert Seddon-Smith:
SELECT id, name, day, key
FROM table
WHERE day > '2013 6-01 00:00:00' && day < '2013-6-31 23:59:59'
GROUP BY YEAR(day), MONTH(day), DAY(day), HOUR(day), SECOND(day)
try SELECT * from table GROUP BY HOUR(date_column) WHERE condition...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
I am trying to select all orders for last 30 days from one customer, so I need to have customer_id = "$customer_id" and count how many orders I have per each day for that one customer.
I need to end up with array like this
Array (
[1] => Array (
[orders] => 41
[date] => 2011-06-13 17:43:50
)
[2] => Array (
[orders] => 11
[date] => 2011-07-13 17:43:50
)
[4] => Array (
[orders] => 2
[date] => 2011-12-13 17:43:50
)
and so on... for 30 days, if some day I dont have any orders, I dont need array or [orders] = 0 ...
}
I have table named "orders" with id, customer_id and date field.
I found this questions SQL query for Calculating Total No. of Orders per Day? but its not helping me, or I dont understand it very well, btw I am beginner. Thanks!
p.s. what I managed to do, is to select all orders for last 30 days.
$this->db->query("SELECT * FROM orders WHERE customer_id=" . $customer['id'] . " AND date > ADDDATE(CURDATE(), INTERVAL -30 DAY)")->result_array();
Use MySQL EXTRACT function to fetch day from your date field and then group by results according to this. I haven't try it but the following query should work:
SELECT COUNT(*) AS orders, date_field
FROM your_table
WHERE customer_id=$my_cusotmer
GROUP BY EXTRACT(DAY FROM date_field)
I have :
$date = $actualite['date'];
$actualite['date'] is a TIMESTAMP
And I was wondering how can I extract from this timestamp the day, then the month, then the year in 3 variables.
Thank you for your help :)
Use date_parse($actualite['date']);, which will return an array containing the day, month, year and other items.
http://www.php.net/manual/en/function.date-parse.php
Example:
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
Output:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
You can extract the values directly within your MySQL query
SELECT DAY( <TIMESTAMP_FIELD> ) AS DAY,
MONTH( <TIMESTAMP_FIELD> ) AS MONTH,
YEAR( <TIMESTAMP_FIELD> ) AS YEAR
FROM <TABLE>
Another way with more options for formatting would be:
$date = date_create($myTimeStamp); // From database "2020-04-09 17:59:20"
$formatedDate = date_format($date, "d/m/y"); // --> 09/04/20
https://www.php.net/manual/en/datetime.format.php
It might be less intuitive than date_parse() but gives you more options as far as I can see.