Hi everyone i have one question search result month in using MySql or Cakephp my table name is state_supplies and data are
MySql query is:
SELECT * FROM `state_supplies` WHERE created=DATE_FORMAT(NOW(), '%m')
Cakephp search code is
$this->StateSupply->find('all', array(
'conditions'=> array(
'unitid'=>$this->Session->read('Auth.User.unitid'),
'created'=>'MONTH(CURDATE())'
)
)
);
Try this:
In Mysql:
$first_day_this_month = date('Y-m-01 H:i:s'); // hard-coded '01' for first day
$last_day_this_month = date('Y-m-t H:i:s');
$sql="SELECT * FROM state_supplies WHERE created between '$first_day_this_month' and '$last_day_this_month' "
Cakephp code
Details
Update code for right answer
$this->StateSupply->find('all',array(
'conditions'=>array(
'unitid'=>$this->Session->read('Auth.User.unitid'),
'crop'=>$this->request->data['StateSupply']['crop'],
'state'=>$this->request->data['StateSupply']['state'],
'created BETWEEN ? AND ?'=>array(date('Y-m-01',strtotime('this month')),date('Y-m-t',strtotime('this month')))
)
)
);
Related
Below code for getting order id for "From" & "to" date but when i execute it gets timeout as i have lots of orders in magento store.
I expect it should return order id of orders created
between the two input dates.
<?php
define('MAGENTO', realpath(dirname(__FILE__) . "/.."));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::init();
Mage::app()->addEventArea('global');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$orderItem = Mage::getModel('sales/order_item')->getCollection();
$orderItem->getSelect()
->joinInner(array('order' => Mage::getSingleton('core/resource')->getTableName('sales/order')), 'order.entity_id = main_table.order_id')
->where("order.created_at >= ?" AND "order.created_at <= ?", '2020-08-23' , '2020-08-25');
print_r(array_unique($orderids));
Any thoughts on this ?
Objective: Trying to get all posts on a certain date from the db table(say, I've datetime in the format '2019-03-07 12:30:00' then I would like to get all posts from this date '2019-03-07').
As I need posts from this date, I'm converting the given datetime to i18nFormat date format. As below ::
$userSelectedDate = $selectedDate->i18nFormat('yyyy-MM-dd');
then, On the where clause, I'm using mySql DATE function on table field and it returns me expected result. code as below:
$conn = connectionManager::get('default');
$conn->begin();
$stmt = $conn->prepare(
"SELECT * FROM `blogs`
where DATE(`PUBLISHED_DATE`) = '$userSelectedDate'"
);
$stmt->execute();
$conn->commit();
This works fine. But I would like to convert it to Cakephp 3 way as below.
$query = $this
->find()
->where([
DATE($this->aliasField('PUBLISHED_DATE')) => $userSelectedDate
])
;
This obviously throws an error as below.
Error: [PDOException] SQLSTATE[42S22]: Column not found: 1054 Unknown column
How to use mysql DATE function in Cakephp 3 query? I've checked other related answers and I couldn't find a way.
Managed to find the answer.
$query = $this->find("all", [
'conditions' => [
'DATE(PUBLISHED_DATE)' => $selectedDate->i18nFormat('yyyy-MM-dd')
]
]
);
I'm trying to get a database entry with this lines of code:
$hoy = date('YYYY-MM-DD');
$stay = Stay::where('guest', '=', $id )
->where('indate', '<=', $hoy )
->where('outdate', '>=', $hoy )
->get( array( 'id', 'room', 'bed', 'guest', 'booking', 'indate', 'outdate' ) );
The thing is, if I remove the outdate >= $hoy line, it works. But with it, it doesn't.
The line i'm trying to retrieve has it's outdate set to 2015-12-02, so it should return it.
Any ideas?
Looks like this is your problem:
$hoy = date('YYYY-MM-DD');
If you want to generate a date like 2015-12-02, you should do this instead:
$hoy = date('Y-m-d');
Source: http://php.net/manual/en/function.date.php
This query expects both conditions to be true.
If you want to return when either one is true you should try ->orwhere.
If you are looking for both to be true you need to keep in mind that if there are timestamps on the date the query of <= will only find dates with a timestamp of 00:00:00 for the requested date.
I have tried to make codeigniter query from mysql query
my mysql query is:
select *
from class_routine
where
semester='$semester'
and day='$day'
and time_schedule='$time_schedule'
and (batch='$batch' or section='$section' or teacher='$teacher' or room='$room');
for above query what will be the codeigniter query ?
I will use this query in model.
First, you should consider looking at the Documentation before asking any question, then specify what you've looked at and what you did try, posting your code.
Anyways, try this and let me know:
$this->db->where(array('semester' => $semester, 'day' => $day, 'time_schedule' => $time_scehdule));
$this->db->where("batch = $batch OR section = $section OR teacher = $teacher OR room = $room", NULL, FALSE);
$result = $this->db->get('class_routine')->result();
When in doubt is perfectly safe to use a normal query:
$result = $this->db->query("SELECT * FROM ....")->result();
I'm currently using Zend Framework 2 and a query with date ranges to obtain data out of a MySQL DB, and I came across the between clause that was previously not available in ZF1.
However, my code which looks something like this is not working correctly:
$dateStart = '2012-12-20';
$dateEnd = '2012-12-31';
$sql = new Sql($_db);
$select = $sql->select()
->from(array("t" => $table))
->columns(array("col1" => "col_as_1", "col2" => "col_as_2"));
$select->where->between("date", $dateStart, $dateEnd);
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute()->getResource()->fetchAll(\PDO::FETCH_ASSOC);
Apparently the between clause is not inclusive, I can only get results until 2012-12-30, is there a way to make it inclusive? I've been taking a look at the ZF2 docs but they are not very helpful and running the same query on MySQL query browser returns all of the data I need.
So you can try lessThanOrEqualTo and greaterThanOrEqualTo.
Between doesn't seem to provide this functionality: between($identifier, $minValue, $maxValue)
If you trace out your query with $select->__toString() you can see the query as string.
I don't have ZF2 on my computer but I could imagine that between in ZF2 will output date > '2012-12-20' AND date < '2012-12-31'.
NOTE THIS : WHEN USING between on Mysql
date_column_name between 'startDate' AND 'endDate'
NOTE : you should want to insert +1 date to endDate . Because of when you insert 2015-05-18 date to endDate.you can not get data of 2015-05-18.So you need to plus one date to endDate.
You can do it using this
$plusToDate = date('Y-m-d H:i:s', strtotime($toDate . ' + 1 day'));
The BETWEEN should be inclusive, are you sure there are no hours, minutes and seconds after the date, that would cause it not to select dates on 2012-12-31 since 2012-12-31 00:00:01 would technically be > 2012-12-31
Format must be same, use mysql DATE function
$select->where->between("DATE(date)", DATE('.$dateStart.'), DATE('.$dateEnd.'));
$from_date = date('Y-m-d', strtotime($AppCleaner->from_date ));
$to_date = date('Y-m-d', strtotime($AppCleaner->to_date ));
$select->where->between('appointment_date', $from_date . ' 00:00:00', $to_date . ' 23:59:59');
also, Use the between clause as below:
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('app_posts');
$select->where->NEST->between( 'id', 30,40);
$select->group('app_posts.id');
// echo $select->getSqlString($this->adapter->getPlatform());
// exit;
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($result);
$posts = $resultSet->buffer()->toArray();
return $resultSet;
Try this:
//I have a static function to make conversion Data Format
public static function convertBrazilianDate2MySQLDatabase($dataBrazil) {
$array = explode("/", $dataBrazil);
$array = array_reverse($array);
$str = implode($array, "/");
return date("Y-m-d", strtotime($str));
}
//In My Service I built my sentence
$dtStart = \Estrutura\Helpers\Data::convertBrazilianDate2MySQLDatabase($dt_start) . ' 00:00:01';
$dtEnd = \Estrutura\Helpers\Data::convertBrazilianDate2MySQLDatabase($dt_end) . ' 23:59:59';
$select->where->between('field name in table', $dtStart, $dtEnd);
[...]