How to find items by date when the field is datetime? - cakephp

I have a model with a DateTime field.
How can I get all entries by Date even if the column is DateTime?
Example table Cars:
id; expiration
1;2014-12-22 00:00:00
2;2014-12-22 03:12:00
3;2014-10-09 01:10:29
If I do this:
$this->Car->find("all", array('conditions' => array('expiration' => '2014-12-22' ) ))
I get only the item with ID 1 (2014-12-22 00:00:00)
But I need all the items (1 and 2)
Any ideas how to accomplish this? in MySql I could do something like this:
SELECT * ... FROM..WHERE DATE_FORMAT(conditions, '%Y-%m-%d') = '2014-12-22'

Try this
$this->Car->find("all", array('conditions' => array('DATE(ondate)' => '2014-12-22') ));
This will generate
SELECT * ... FROM..WHERE WHERE DATE(ondate) = '2014-12-22'

You need to update your query as shown below:
$this->Car->find("all", array(
'conditions' => array(
'DATE(expiration)' => '2014-12-22'
)
)
);
In the above query, Mysql now compare only date not the date and time as it was using in the query mentioned by you.

You will need to search trough a range: 00:00:00 - 23:59:59
http://cookbook.mongodb.org/patterns/date_range/

Related

from date and to date not between in cakephp

I have two dates 30-01-2017 & 02-02-2017
Need a condition in cakephp to search record these two dates doesn't lies in between.
Please use this.I think this would be helpful.
$searchFirstdate = '30-01-2017';
$searchLastdate = '02-02-2017';
$this->ModelName->find(
'all',array(
'conditions'=>array(
'ModelName .date_field >= ' => $searchFirstdate,
'ModelName .date_field <= ' => $searchLastdate
)
)
);
There is a NOT BETWEEN operand for MYSQL, so you could easily use:
$this->ModelName->find('all', array(
'conditions' => array(
'ModelName.date_field NOT BETWEEN ? AND ?' => array('30-01-2017', '02-02-2017')
)
));
From CakePHP Manual

date range check in single field date in cakephp

suppose we have $start-date = 2013-09-23 and $end-date = 2013-09-28 in controller. Now we have to get such field's date which are in between this range. example : table field Modified date : 2013-09-22 then this will be output.
Your question it's not so clear but, if I understand, your asking something like:
$this->YourModel->find->(
'all',
array(
'conditions' => array(
'YourModel.modified BETWEEN ? AND ?' => array($start-date, $end-date)
)
)
);

findall ignoring order parameter?

I'm trying to use the following:
$this->Chapter->recursive=1;
$chaps = $this->Chapter->find('all', array(
'order'=> array('sequence_number' => 'ASC')
));
$this->set('chapters', $chaps );
to retrieve all my chapters by increasing order, but CakePHP seems to be ignoring the 'order' parameter. I believe that I have the syntax correct (based on view-source:http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types, which says the following should work:
public function index() {
$articles = $this->Article->find('available', array(
'order' => array('created' => 'desc')
));
}
). The SQL for the table looks like:
CREATE TABLE chapters (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sequence_number INT UNSIGNED,
title VARCHAR(50)
);
and I'm not getting any syntax or run-time errors. However, the SQL generated by Cake to actually get the chapter records is:
SELECT `Chapter`.`id`, `Chapter`.`sequence_number`, `Chapter`.`title`
FROM `Tutorial`.`chapters` AS `Chapter` WHERE 1 = 1
Clearly I'm doing something wrong, but I don't know what it is.
As a work-around I'm happy to put an order property on the Model. Since I typically want to retrieve chapters by sequence number I'm fine with adding this to the model:
public $order = 'Chapter.sequence_number ASC';
Once I do that Cake generates
SELECT `Chapter`.`id`, `Chapter`.`sequence_number`, `Chapter`.`title`
FROM `Tutorial`.`chapters` AS `Chapter` WHERE 1 = 1
ORDER BY `Chapter`.`sequence_number` ASC
What if you try something like this, instead:
$chaps = $this->Chapter->find('all', array(
'order' => array('Chapter.sequence_number ASC')
));
The main difference being this part: Chapter.sequence_number ASC

CakePHP : Search date from form helper

I use form helpers to show drop down select date. My problem is, i can't compare date from ms sql with form data.
My View :
<?php echo $this->Form->input('collect_date', array('label' => 'Collect Date','type'=>'date','dateFormat'=> 'DMY','minYear' => date('Y'),'maxYear' => date('Y')+1));?>
My Controller
$status =array(0,2,4);
$find_date = $this->data['Transaction']['collect_date'];
$field = array('Transaction.status','Catalogue.title', 'Catalogue.author', 'Catalogue.isbn', 'Location.rack');
$condition = array('OR' => array('AND' =>array('Transaction.return_date <'=> $find_date,'Transaction.status '=> '3'),array('Transaction.status'=> $status)));
$data = $this->Catalogue->Transaction->find('count',array('fields' => $field,'conditions'=>$condition,'order'=>array('Transaction.id desc') ));
$this->set('Found', $data);
And the sql dump
SELECT COUNT(*) AS [count] FROM [transactions] AS [Transaction]
LEFT JOIN [catalogues] AS [Catalogue] ON ([Transaction].[catalogue_id] = [Catalogue].[id])
LEFT JOIN [users] AS [User] ON ([Transaction].[user_id] = [User].[id])
WHERE (((([Transaction].[return_date] < ('01', '09', 2013))
AND ([Transaction].[status] = 3))) OR ([Transaction].[status] IN (0, 2, 4)))
As you can see the date format ('01', '09', 2013). But when try to convert use this
'Transaction.return_date <'=> date('Y-m-d', strtotime($find_date))
it show error:
Warning (2): strtotime() expects parameter 1 to be string, array given [APP\Controller\CataloguesController.php, line 66]
and the sql show:
[Transaction].[return_date] < '1970-01-01'
You could use:
$date = DateTime::createFromFormat('d/m/y', implode('/', $find_date));
'Transaction.return_date <'=> $date->format('Y-m-d');
Edit:
I think the way its meant to be 'flattened' is with
$this->Model->deconstruct('find_date', $find_date);
However, last time i tried to use this, i couldn't get it to work properly, so i went with the above.
http://api21.cakephp.org/class/model#method-Modeldeconstruct
Try converting return date using UNIX_TIMESTAMP in sql query and then comparing it with
strtotime($find_date).

How can I query data with two date ranges in CakePHP?

I'm trying to execute this query to fetch 'Banners' between a date_start and a date_end.
$current_date = date('Y-m-d');
$banners = $this->Banner->find('all',
array('conditions' =>
array("date_start >= " => $current_date,
"date_end <= " => $current_date)
));
I've attempted to use NOW() which seems to cause problems, I've tried using an "AND" condition and I've also concatenated the query with $current_date (eg. "date_start =>".$current_date)
Any ideas where I'm going wrong?
Edit
Managed to get it working by switching the conditions around:
$banners = $this->Banner->find('all', array('conditions' => array("'$current_date' >=" >= "date_start", "'$current_date' <=" => 'date_end')));
you should change your equation
date_start <= $current_date and
date_end >= $current date
lets say date_start is 1/5/2011
and date_end is 3/5/2011
and curr_date is 2/5/2011
notice that curr_date is bigger than start_date and smaller than end_date, in your condition you check the opposite

Resources