how to get all products between two dates? - magento

how to get all products between two dates like last month products, this month products , last week products and this week products etc.
i tried with this:
// current day to start with
$start = mktime(0,0,0,date('m'), date('d'), date('Y'));;
// calculate the first day of last month
$first = date('YYYY-MM-DD',mktime(0,0,0,date('m',$start) - 1,1,date('Y',$start)));
// calculate the last day of last month
$last = date('YYYY-MM-DD',mktime(0, 0, 0, date('m') -1 + 1, 0, date('Y',$start)));
if($filter == "lastmonth"){
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('updated_at', array('gteq' =>$first));
$collection->addAttributeToFilter('updated_at', array('lteq' => $last));
}
but i am not able to get the result :( any help ?
Modified after Daniel response !

1) First of all you need to change you date formate from 'YYYY-MM-DD' to 'Y-m-d'. This will return a date formate which magento records have.
2) There is a special condition for date as bellow mention with your example.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('updated_at', array('gteq' =>$first));
$collection->addAttributeToFilter('updated_at', array('lteq' => $last));
To.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('updated_at', array(
'from' => $first,
'to' => $last,
'date' => true,
));

I tried your code and had to swap 'lteq' and 'gteq' to make it work. The $fromdate is the lower number so you are searching for dates greater than that number.
Also you must remember to format the dates as MySQL likes it; date('Y-m-d').
PS. See the comparison operators for a full list

There is one issue with your code:
$collection->addFieldToFilter()
should be:
$collection->addAttributeToFilter()

I know that the question is little bit old but as it is quite well ranked in search engine results, I will correct the date() function that better take as arguments something like : Y-m-d H:i:s.
I hope it will help !

Related

Laravel carbon retrieve record even when it shouldn't

I am searching by start and end date or with a weekday, which kind of works fine however if weekday is equal to 'WE' and today's weekday is equal to 'WE' it still brings back results even when $today date is < than endate.
public function show($id)
{
$weekMap = [
0 => 'SU',
1 => 'MO',
2 => 'TU',
3 => 'WE',
4 => 'TH',
5 => 'FR',
6 => 'SA',
];
$todayWeek = Carbon::now()->dayOfWeek;
$today= Carbon::now();
$weekday = $weekMap[$todayWeek];
$event = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'))
->orWhere('weekday', '=', $weekday)
->get();
dd($event);
return view('events.showEvent', compact('event'));
}
If I change orWhere to where, results are all good, however user not always enters a weekday so it can be empty. I want it to work like this:
output everything where startdate < today and where endate > today and if weekday is not empty, output everything where startdate < today, endate > today and weekday = $weekday. I hope that makes sense.
//edit
sample data:
data
So what I want is really search by frequency so it would look like this:
if frequency = daily
compare start and end with today's date.
if frequency = weekly
compare start and end with today's date and weekday.
bring back all results for frequency = daily+weekly
you can use if condition in query like this:
$query = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'));
if ($weekday != '') {
$query->where('weekday', $weekday);
}
$event = $query->get();
also
condition if ($weekday != '') check $weekday isset or not you can use
if ( ! is_null($weekday )) instead

Laravel - after date validation with date from rules set

I'm trying to validate two dates to ensure one is greater than the other using the after validate rule. I have my rules set like this:
$rules = array(
'date_from' => 'required|date_format:"d/m/Y"',
'date_to' => 'required|date_format:"d/m/Y"|after:date_from',
// more rules ...
);
When using the following values:
date_from = "01/06/2014" and date_to = "01/06/2014" OR date_to = "12/06/2014" everything is hunky dory, however.. using anything above 12 for day fails i.e. date_to = "13/06/2014" to date_to = "31/06/2014"
I've also tried this and it gives the same results:
$dateFromForm = Input::get('date_from');
$rules = array(
'date_from' => 'required|date_format:"d/m/Y"',
'date_to' => 'required|date_format:"d/m/Y"|after:' . $dateFromForm,
);
Quite clearly to me it's reading the day as the month, any ideas what I'm doing wrong here?
Thanks
If you look at the Laravel docs - it says
The dates will be passed into the PHP strtotime function.
The problem with strtotime is that it assumes you are using m/d/Y. If you want d/m/Y - you need to change it to d-m-Y to be correctly parsed.
So change your date format to d-m-Y and it will work.

Getting daily aggregates/sum sorted by day in Laravel

So getting a sum()/count() is really easy in Laravel...
but how would I look at the past month, and get the sum of rows every day?
EG...grouped by day that they were created at.
So I want to return a count such as 3, 2, 4, 5
Meaning 3 rows were created on todays date, 2 rows yesterday, 4 rows the day before...etc
How to do this in Laravel easily?
When I use the group by created_at it always just returns 1.
Anybody know how to do it?
Thanks
I've provided the same answer on another post. Shortening it:
$date = new DateTime('tomorrow -1 month');
// lists() does not accept raw queries,
// so you have to specify the SELECT clause
$days = Object::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`')
))
->where('created_at', '>', $date)
->group_by('date')
->order_by('date', 'DESC')
->lists('count', 'date');
// Notice lists returns an associative array with its second and
// optional param as the key, and the first param as the value
foreach ($days as $date => $count) {
print($date . ' - ' . $count);
}

Magento order created_at wrong date

I'm trying to get all orders in magento, from a specific user entered date.
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from' => $userdate));
But its not pulling out the correct orders. If I use today's date, It pulls orders half from yesterday and half from today.
After a bit of Googling, it looks like there's two dates stored in Magento
$order->getCreatedAt()
Seems to give a UTC/GMT time
$order->getCreatedAtStoreDate()
Gives the same time as I see in the frontend (ie. my local timezone).
If I'm correct in what I have found, then how do I addAttributeToFilter using the CreatedAtStoreDate. I have tried
('created_at_store_date', array('from' => $userdate)
<?php
ini_set('display_errors',true);
include 'app/Mage.php';
Mage::getIsDeveloperMode(true);
Mage::app();
$formatStr = 'Y-m-d H:i:s';
$startStr = 'Yesterday 12:00:00AM';
$endStr = 'Yesterday 11:59:59PM';
$date = Mage::getSingleton('core/date');
/* #var $date Mage_Core_Model_Date */
$collection = Mage::getResourceModel('sales/order_collection');
$collection->getSelect()
->where(
sprintf("created_at BETWEEN '%s' AND '%s'",
$date->gmtDate($formatStr,$startStr),
$date->gmtDate($formatStr,$endStr)
)
);
$collection->load(true);

magento getCollection() can we put where condition?

i am working on a magento project and i need to get the value according to country wise like select address where country ="Nepal"
can we send the where condition in getCollection() function
$collection = Mage::getModel('relocator/location')->getCollection();
any help will be appreciated
got the solution
;P
Mage::getModel('relocator/location')
->getCollection()
->addFilter('country','Nepal');
You can use diffrent condition like below reference from this.
$collection = Mage::getModel('catalog/product')->getCollection();
Is Equal To
$collection->addAttributeToFilter('status', array('eq' => 1));
Greater Than
$collection->addAttributeToFilter('price', array('gt' => 3));
Contains – with % wildcards
Is NULL
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,12)));

Resources