Okay, So I am needing to retrieve the rows in a database table that where inserted the previous month of the current. shop_orders
is the database.
I do have a datetime
in each row for the order date or o_date
and that will be used to determine when it was entered.
I came up with this function in codeigniter model. The first portion in the code determines what the first day of last month is and puts into the same format that is stored in the database. The second portion retrieves the information from the database. I'm just not sure what to place in the where
line to retrieve the results only from the first day of last month to the last day of last month.
The first portion of the below code outputs just like this:
Today is: 2014/05/30 the first day of last month was: 2014/04/01 and the last day of last month was: 2014/04/30
.
I need to tell the second block:
Select * from shop_orders where o_date is between 2014/04/01 and 2014/04/30
Obviously that isn't correct sql, what is? My real question.
function get_last_month_order_count()
{
$date = date('Y/m/d');
$today = new DateTime( $date );
$today->modify( 'first day of last month' );
$firstDay = $today->format( 'Y/m/d' );
$today = new DateTime( $date );
$today->modify( 'last day of last month' );
$lastDay = $today->format( 'Y/m/d' );
$this->db->select('*');
$this->db->from('shop_orders');
$this->db->where('o_date', '');
return $this->db->count_all_results();
}