This question already has an answer here:
Sum hours from time tracker to calendar by day
(1 answer)
Closed 4 years ago.
I need help on how to group my QUERY output by week.
The current output looks like this:
What I want:
I'd like it to sum all the columns that contain the same week number and hours into one column.
My current QUERY:
$query = "
SELECT userhours_id, user_hours, hours_timeoccur, SUM(user_hours) as 'myhours' FROM hours h
where h.userhours_id = $loginuser_id
AND h.hours_timeoccur = '$datetime'
";
I did try add the following to group it by $datetime which is each days number in 20xx-xx-xx format but it didn't make any difference:
GROUP BY WEEK(WEEKOFYEAR(h.hours_timeoccur))
I also tried to add an PHP IF statement which said if $weeknum (which is the week of each date), but I never really figured out what parameters I would be use.. so got stuck in this:
if ($weeknum = $weeknum ) {
'myoutput'
}
Any idéa on how to accomplish this kind of request? Thanks in advance!
This query should give the data grouped by user and year week:
SELECT userhours_id, WEEKOFYEAR(h.hours_timeoccur) AS hours_week, SUM(user_hours) as 'myhours'
FROM hours h
WHERE h.userhours_id = $loginuser_id
GROUP BY userhours_id, hours_week
I removed the date where clause as if you only select for one date then it would not show useful information, maybe it would be best to provide a date range and query BETWEEN?
Related
This question already has answers here:
Get closest date from MySQL table
(3 answers)
Closed 7 years ago.
Hi i have some examples dates given below.How to find the next closest date in php or in mysql ?
I have some date like 12-04-2015 so now i need to get the closest date after the given date.So in my case my closest date is 15-04-2015.How can i find using PHP and mysql ?? Any one help me
PHP
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
Mysql
SELECT Birthdate FROM hedging ORDER BY ABS(DATEDIFF(Birthdate , `2015-04-12`)) LIMIT 1
How can i execute the above query in mysql ?
BirthDate
25-03-2015
10-04-2015
10-04-2015
11-04-2015
15-04-2015
30-04-2015
You've not given us much information about what you've tried.
In PHP, store the dates in an array, you can then simply sort it and choose the next value.
In SQL so long as they are stored as dates, then WHERE date > $date ORDER BY date DESC LIMIT 1 will give you the next date.
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 7 years ago.
I have a table in the database with the name attendance. It has three columns: intime, outtime and work_hours.
I have date and time in intime and outime. Now I want to write the php code to calculate the time difference in the format hh:mm:ss to store into the column work_hour of attendance table.
Please help me out.
timediff returns the difference between two datetimes as a time value:
UPDATE attendance
SET work_hours = TIMEDIFF (outtime, intime)
I have one simple solution, but you will find better. Just try this.
For this you need to convert both of your outtime and intime in seconds using
$working_time_in_seconds = strtotime($outtime) - strtotime($intime);
echo date('H:i:s', $working_time_in_seconds );
This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Closed 9 years ago.
I have a table in database for meeting schedules. It has 2 columns named start and end and since I dont have access to the php script which fills this table with new data, I am not sure in which format it is.
But PHPMyAdmin shows taht the columns for start and end are varchar(15) So I guess it should be datetime compatible.
Example in DB: 1378033200
Which shows as 01 September 2013
My question is, I want to pull the meetings and show them in a html page, but I do not want meetings which are older than 2 days ago (server time) to show up. What will be the query?
SELECT * FROM schedules ORDER BY start
Something like
SELECT * FROM schedules ORDER BY start WHERE start > 2 days ago
I tried this but it seems it does nothing!
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < CURDATE() - INTERVAL 2 DAY
ORDER BY start
But PHPMyAdmin shows taht the columns for start and end are
varchar(15) So I guess it should be datetime compatible.
You've guessed wrong. Strings are only sortable as strings. Which means, unless you're using a sortable date formats (YYYY/MM/DD being one: I'm not aware of others) you'll have to parse all the results and do the calculation by yourself in PHP (otherwise, 13/11/2000 will come before 14/01/2000). Alternatively, you might wanna use the proper type for your column: datetime, date or timestamp. Once you'll do that, you'll be able to query your db and compare dates with < and > operators.
For the 2 days ago part, you'd like to know that MySql has a built in NOW variable to which you can sum/subtract days. If you'll design your db correctly, you won't even have to touch PHP (which a desiderable thing).
Try this:
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < DATE_SUB(NOW(), INTERVAL 2 DAYS)
ORDER BY start
What I am trying to do here is render a report for each date in the current month from a MYSQL table.
I have a table with rows in, which a lot have the same date. As you can see here
What I would like to do is for every date in the current month is see how many rows have been found in the MYSQL table for that date and then return it into an array. The final product will be a multidimensional array for every date in the current month. Something like this
array("dates"=>
array(
"1 feb"=>2, //the number of rows for that date found in the MYSQL table
"2 feb"=>8,
"3 feb"=>0
)
)
But the issue is I wouldn't have a clue where to start with the coding, like what PHP functions would I use? So I was hoping someone could push me in the right direction :)
Probably not working code but should point you in the right direction:
SELECT
SUM(money) AS sum,
`date`
FROM
money_table
WHERE
MONTH(`date`) = 2
GROUP BY
DAY(`date`)
Reference
GROUP BY
MONTH
DAY
You said you didn't have a table with all the dates. If you did, this would be trivial. Let's say you had such a table called calendar.
select calendardate
, ifnull(sum(money), 0) sum_of_money
from calendar left join money on calendardate = money.date
where calendardate >= {d '2013-02-01'}
and calendardate < {d '2013-03-01'}
group by calendardate
Other things that could be stored in the calendar table are fiscal information and holidays. It might be worth your while to create and maintain one.
Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";