how to compare last week and last month date functions - php

$start = strtotime('this week');
$results =$wpdb->get_results("SELECT count( doctor_name ) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date >='". $start."'");
this is my code to get last week leads count from table doctor name and where date with in this week (means today is thusday then start from previous week)
it not working??
and have do same for function like last month ??
in my db i use this leads_date field as timestamp

you can use the date_sub function from mysql
get all records from last week
SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 WEEK) and now()
get all records from last month
SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 MONTH) and now()

try
$daynumber = date('N', date('d'));// getting today day number
$prevweek = $daynumber+7; // starting from prev week
echo $prevdate = strtotime('-'.$prevweek.' days'); // prev week date
echo strtotime("-1 month"); // last month
For more :-
Getting last month's date in php
day of the week to day number (Monday = 1, Tuesday = 2)

Related

How to pull data from database table for previous month only

I want to show records for previous month only, excluding this month's dates.For example, today is February 5th and I want to show records for January 1st to 31st
i have a table- tbl_order_details where I need to fetch all order records by current month and previous month respectively. the column name for date type is orderDate this is what I an doing for fetching rows for current month till date:
SELECT COUNT(1)
FROM tbl_order_details
where merchantCode= '$user_code'
AND MONTH(orderDate) = MONTH(CURRENT_DATE())
AND YEAR(orderDate) = YEAR(CURRENT_DATE())
But I cant figure out how do I show records for january that does not include any records from February
SELECT * FROM tbl_order_details
WHERE YEAR(orderDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(orderDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
For writing a PHP code, you can get the Month and Year from PHP itself by using the strtotime function depending upon the input your table takes and then formatting it in your sql query. For eg.:
<?php
$month = date("M", strtotime("previous month"));
$year = date("Y", strtotime("this year"));
$query_get = 'SELECT COUNT(1) FROM tbl_order_details where merchantCode= {$user_code} AND MONTH(orderDate) = {$month} AND YEAR(orderDate) = {$year}'
?>
And further pass $query_get to your DB query to fetch the required result. Or else, you can straight push the following query as #Rohit suggested above.
<?php
$query_get = 'SELECT * FROM tbl_order_details WHERE YEAR(orderDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(orderDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)'
?>
Avoid using DATE (), MONTH (), DAY (), YEAR (), SUBSTR (), LEFT (), RIGHT (), LIKE when mentioning columns in WHERE or JOIN'S because you no longer use the indexes that exist in the columns mentioned. Ex: WHERE YEAR(orderDate) = ... Avoid doing this for the reasons stated above.
I suggest use as follows ...
If your "orderDate" column is of type date, do as follows:
SELECT COUNT(1)
FROM tbl_order_details
where merchantCode= '$user_code'
AND orderDate BETWEEN DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY)
AND LAST_DAY(DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY));
Will return the first day of the previous month
DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY)
Returns the last day of the previous month
LAST_DAY(DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY))

SUM multiple date ranges in mysql query

I have a MySql table 'Products' with three columns:
Date | Product | Qty
My aim is to SUM the qty of each product for every week.
Getting the SUM between two given dates would be easy:
SELECT SUM(qty) FROM products WHERE date >= '2010-01-01' AND date < '2011-01-1'
I can generate a list of weeks in Php using something like:
$today = date('Y-m-d', strtotime('last week Monday'));
$future = date('Y-m-d', strtotime('+90 days'));
$period = new DatePeriod(
new DateTime($today),
new DateInterval('P7D'),
new DateTime($future)
);
foreach ($period as $key => $value) {
$dates .= $value->format('Y-m-d');
}
Is there a MySql query that would work to Group By and SUM by dates? Or would I be better off looping through in Php?
You can use Year() and Week() functions in MySQL, to get the year and week number for a given date. Week() function will return week number from 0 to 53. So, you will be needed to use Year() function alongwith, if you have data spanning over multiple years.
But then, you will be more interested in knowing the start date and end date for the concerned week. This is where we can use a very interesting function DayOfWeek(). It returns the weekday index for a given date (1 = Sunday, 2 = Monday, …, 7 = Saturday)
We can use Date_Add() function using the weekday index value and the actual date value, to determine the starting week date and ending week date for a given date.
Try the following (if the Week starts on Sunday) :
SELECT
DATE_ADD(`date`, INTERVAL(1 - DAYOFWEEK(`date`)) DAY) AS week_start_date,
DATE_ADD(`date`, INTERVAL(7 - DAYOFWEEK(`date`)) DAY) AS week_end_date,
SUM(qty)
FROM
products
GROUP BY week_start_date, week_end_date
If the week starts on Monday, another handy function is WeekDay(). It returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
Try the following (if the Week starts on Monday) :
SELECT
DATE_ADD(`date`, INTERVAL(0 - WEEKDAY(`date`)) DAY) AS week_start_date,
DATE_ADD(`date`, INTERVAL(6 - WEEKDAY(`date`)) DAY) AS week_end_date,
SUM(qty)
FROM
products
GROUP BY week_start_date, week_end_date
You can achieve that with a group by like
GROUP BY week(date)
GROUP BY so do
SELECT SUM(qty) FROM products GROUP BY WEEK(date);

How to print current month in Mysql DateTime format in PHP

how would I go about echo'ing the first and last day of the current month? I assume I would use mktime() but I am slightly confused by it.
For example the current month is June so I am looking for a way to echo:
2016-06-01 00:00:00
And
2016-06-30 00:00:00
Thanks in advance.
You can use DateTime for this.
<?php
$start_date_month = new DateTime("first day of last month");
$end_date_month = new DateTime("last day of last month");
echo $start_date_month->format('Y-m-d');
echo $end_date_month->format('Y-m-d');
?>
Here is one method:
select date_sub(curdate(), interval 1 - day(curdate()) day) as month_start,
date_sub(date_add(curdate(), interval 1 month), interval - day(curdate()) day) as month_end
SELECT ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)),1) first,
LAST_DAY(CURDATE()) last

How do I query Last Week From Thursday to Wednesday?

I am trying to get a query to pull records from last week from thursday morning at 12:00AM to Wednesday night at 12:00pm.
This needs to always run and be current. I have another query for the current week from thursday to wednesday and it actually works. I just need another query that shows results after the week has passed from last week.
I believe this query is for last week but not starting on Thursday and ending on Wednesday.
SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo
WHERE thedate >= Curdate() - INTERVAL Dayofweek(Curdate())+6 day
AND thedate < Curdate() - INTERVAL Dayofweek(Curdate())-1 day
GROUP BY collector ORDER BY totalsum DESC
There might be a better way to do this using MySQL's own date functions, but I think I would probably just do it like this. I'm assuming that you meant Wednesday night at 11:59:59pm though, not noon:
$lastThursday = new DateTime("Thursday last week");
$wednesday = new DateTime("Wednesday");
$today = new DateTime("now");
$day = $today->format("l");
if ($day == "Sunday" || $day == "Monday" || $day == "Tuesday" || $day == "Wednesday") {
$lastThursday->modify("-1 week");
$wednesday->modify("-1 week");
}
$query = "SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo WHERE thedate BETWEEN ".$lastThursday->format("Y-m-d")." AND ".$wednesday->format("Y-m-d ")." GROUP BY collector ORDER BY totalsum DESC";

PHP Mysql GET Last month and current month data

I want to retrieve m last month data and current month data
my query works correctly as
last month data calculate 1 month from current date. But i want my data calculate as calander month not from my current date.
like
my current date is 2014-07-23
now my calculation return data by calculating month from my current date.
but I want to get based on calendar jan,feb,march range.
my current query of last month
SELECT (100*AVG((m.carbs)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_carbs,(100*AVG((m.fat)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_fat,(100*AVG((m.protein)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_protein
FROM `meal` AS m,`user_history` as u where u.meal_id=m.id and u.user_id=$user_id and date(FROM_UNIXTIME(u.create)) BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
my current query of this month
SELECT (100*AVG((m.carbs)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_carbs,(100*AVG((m.fat)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_fat,(100*AVG((m.protein)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_protein
FROM `meal` AS m,`user_history` as u where u.meal_id=m.id and u.user_id=$user_id and YEARWEEK(date(FROM_UNIXTIME(u.create))) = YEARWEEK(CURRENT_DATE)
Last month
year(date(FROM_UNIXTIME(u.create))) = year(CURDATE() - INTERVAL 1 MONTH)
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE() - INTERVAL 1 MONTH)
THis month
year(date(FROM_UNIXTIME(u.create))) = year(CURDATE())
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE())

Categories