I'm new to Drupal 7, and I have a problem where the events calendar week view doesn't show Sunday events, because the end date in the query is incorrect. Actually it only ever shows 6 days. How can I modify the query? How can I even find where it is generated?
Below is the query from the Events Calendar (Content) page. You can see that the start date comparison is ">= '2016-11-14 00:00:00" end date comparison is "<= '2016-11-20 00:00:00'", which doesn't retrieve events happening on Sunday. I need to change it to "<= '2016-11-20 23:59:59'".
Changing the first day of the week doesn't help, because it just shifts the six day time frame to start on Sunday, and misses events on both Saturday and Sunday.
SELECT field_data_field_event_date.delta AS field_data_field_event_date_delta, field_data_field_event_date.entity_id AS date_id_field_event_date, field_data_field_event_date.delta AS date_delta_field_event_date, node.nid AS nid, field_data_field_event_date.language AS field_data_field_event_date_language, field_data_field_event_date.bundle AS field_data_field_event_date_bundle, field_data_field_event_date.field_event_date_value AS field_data_field_event_date_field_event_date_value, field_data_field_event_date.field_event_date_value2 AS field_data_field_event_date_field_event_date_value2, field_data_field_event_date.field_event_date_rrule AS field_data_field_event_date_field_event_date_rrule, node.title AS node_title, 'node' AS field_data_field_event_date_node_entity_type, 'node' AS field_data_body_node_entity_type, 'node' AS field_data_field_event_cat_node_entity_type
FROM
{node} node
LEFT JOIN {field_data_field_event_date} field_data_field_event_date ON node.nid = field_data_field_event_date.entity_id AND field_data_field_event_date.entity_type = 'node'
WHERE (( (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%d %H:%i:%s') >= '2016-11-14 00:00:00' AND DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%d %H:%i:%s') <= '2016-11-20 00:00:00') )AND(( (node.status = '1') )))
ORDER BY field_data_field_event_date_field_event_date_value ASC
I would first check your regional settings asyou can change the First day of the week there and just resave the value there - possibly try ye olde on-and-off again, switch between monday and sunday. /admin/config/regional/settings
Secondly, have you checked the module itself for this issue? A patch may be posted under their issue queue.
If all else fails, you could use hook_views_query_alter in a custom module to modify the where clause of the query that is generated. I've written a blog post on this hook previously found here but basically you have all the components of the query so can alter what you need there. Eg.
// add the condition of greater than current timestamp
$date = gmdate('Y-m-d H:i:s');
$view->query->where[1]['conditions'][] = array(
'field' => 'field_mytimefield_value',
'value' => $date,
'operator' => '>='
);
Related
I am creating the lightning flow. In which, I need to provide the date in the 'field1' (Type : Date) in the screen component.
In the screen component, the 'field1' should only accept the date value which is in the range of 3years in the past of the current date and 2 years in the future of the current date.
For instance, if a user is entering the value in the 'field1' Date field on 5/13/2021 then the valid date range is between 5/13/2018 thru 5/13/2023.
Can anyone please guide/assist on how to achieve this.
Thanks in advance.
enter image description here
To create this date field you need to use following formula
IF(OR(
({!Accepted_Date} <= (DATE(YEAR({!$Flow.CurrentDate}) - 3,MONTH({!$Flow.CurrentDate}),DAY({!$Flow.CurrentDate}) - 1))), ({!Accepted_Date} >= (DATE(YEAR({!$Flow.CurrentDate}) + 2,MONTH({!$Flow.CurrentDate}),DAY({!$Flow.CurrentDate}) + 1)))
), false, true
)
So I am building a project where I need to fetch date from the database and on the basis of the given month,
as I need to find customers who have birthday on the given month. I have a query that works perfectly fine in the SQL server but I don't get its equivalent in Linq.
So here is my query that runs in SQL
SELECT * FROM CustReg
WHERE DOB LIKE '_____month%'
I need to match that month as it should come in the 6th position of that DOB string.
Demo Database-
Id Name DOB
1 AB 1995-02-20
2 CD 1998-04-13
4 EF 1991-02-15
5 GH 1988-06-8
6 IJ 2000-02-09
Query - Select all Employee whose birthday comes on feb.
Expected Output - AB,EF,IJ.
PS- I have taken datatype of DOB string not Date or DateTime.
I also have month in string (for eg '02' for feb)that I have fetched from the input date.
If I have understood your question right, may be the following code helps.
First create a enum of Months,
public enum Month
{
Jan=1,Feb,Mar,Apr,May,Jun,July,Aug,Sep,Oct,Nov,Dec
}
Now use the following query to get what u wanted,
Month requiredMonth = Month.Feb;
var res = CustReg.Where(x => (Month)Enum.Parse(typeof(Month), x.DOB.Substring(5, 2)) == requiredMonth).Select(x => x.Name);
foreach (var item in res)
{
Console.WriteLine(item);
}
Look at the above image.
What I want is create a table of weekly data from using above db.
From past week the (from today to 7 days before). The table should contain dish_name , Sum of quantity for past week, sum of price for past week group by dish_name.
Can anyone give a solution for this. (Using Laravel 5.7)
$today = Carbon::now();
$one_week_ago = Carbon::now()->subWeeks(1);
$something = BillPaid::where('created_at', '>=', $one_week_ago)
->where('created_at', '<=', $today)
->select('dish_name', 'quantity')->groupBy('dish_name')->get();
The Problem isn't getting dates. I can clearly get products from above query. I have no idea what I have to do after that. From above image, I attached, the past week goes from 2018-12-28 to 2018-12-21 so all records from that image should includes.
What I want is make a table like:
"vege rice" , 3 (quatation), 600(price) ,
"Chicken Fried rice", 9(quatation), 3150 (price)
Try this code:
$today = Carbon::now();
$one_week_ago = Carbon::now()->subWeeks(1);
$something = BillPaid::selectRaw('dish_name,SUM(price) as totalPrice,SUM(quantity) as totalQuantity')
->whereBetween('created_at', [$one_week_ago, $today])
->groupBy('dish_name')->get();
I am using CakePHP 3.x. I have converted and stored all my dates to string using the strtotime(date('Y/m/d h:i', time())); .And in DB it will store like this 1479466560 But Right now i need to select rows from exactly 14 days ago. I tried like below conditions,
$this->loadModel('Users');
$userList = $this->Users->find('all')
->select(['created'])
->where(['status' => 1,'created' => 'created + INTERVAL 14 DAY > NOW()']);
It is returning empty rows only. How to do this ?. Can any one help me !
I know this does not tell you how to use interval but it does solve the problem.
What I've done in the past is a simple >= < combination together with php variables:
$cur_date = date_create();
$date = clone $cur_date;
$date->modify("-12 hours");
$this->Model->find()
->where(['created >= ' => $date])
->andWhere(['created < '=> $cur_date]);
This effectively uses an interval. It is also nicely dynamically editable.
Your query doesn't match with any created date from your users table.
Your CakePHP3 query generated below SQL:
SELECT created FROM users WHERE (status = 1 AND created = created + INTERVAL 14 DAY > NOW())
Note: At First try and adjust above query from your phpmyadmin.
You can try below SQL for INTERVAL test
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY > NOW();
OR
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY;
Note: Adjust date string as you need. Now you can see what is the wrong you have done
I need to count the users, but my condition is only if their account have been created today. I have a users table with a created field (datetime) for each rows. How can i do it in Cakephp, i didn't find the answer in the documentation.
$usersNewCount = Number::format($this->Users->find()->where(['created' => 'CURDATE()'])->count());
I tried with CURDATE, and of course it's not working, i guess Cakephp has a specific function for te datetime field ?
What you are doing there won't work for various reasons.
You cannot pass SQL snippets in the value part of the conditions array, it will be escaped and you'll end up with a string comparison like created = 'CURDATE()', you'd either have to pass the whole condition as a string, or use raw expressions.
Even when properly passing CURDATE(), the comparison won't work as the created column has a time part.
While it is possible to circumvent the former problem by transforming the column, you should try to avoid that whenever possible! Comparing to calculated columns like DATE(created) = CURDATE() will make using indices impossible, and thus massively degrade performance!
So unless you have an extra column that holds just the date part, your best bet is a BETWEEN comparison which is the equivalent to a >= x AND a <= y, and in order to stay cross DBMS compatible, this is best to be done by passing dates from PHP, ie not using DBMS specific date and time functions like CURDATE().
$this->Users
->find()
->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
$from = (new \DateTime())->setTime(0, 0, 0);
$to = (new \DateTime())->setTime(23, 59, 59);
return $exp->between('Users.created', $from, $to, 'datetime');
})
->count()
This will create a query similar to
SELECT
(COUNT(*)) AS `count`
FROM
users Users
WHERE
Users.created BETWEEN '2015-05-26 00:00:00' AND '2015-05-26 23:59:59'
See also
API > \Cake\Database\Expression\QueryExpression::between()
You can do it this way
$usersNewCount = Number::format($this->Users->find()->where([
'DATE(created) = CURDATE()'
])->count());
Note that passing it in form where(['DATE(created)' => 'CURDATE()']) will not work, since CURDATE() will be interpreted as a string.
When doing 'created' => 'CURDATE()' you are checking for a complete match, getting '2015-05-26', without a time. You need to check for a time interval:
$usersNewCount = Number::format(
$this->Users->find()->where([
'created >=' => date('Y-m-d').' 00:00:00',
'created <=' => date('Y-m-d').' 23:59:59'
])->count());