I've got a working calendar function, and a working sum to calculate the project hours from the time tracker, but I'm struggeling to combine them into a working function.
See comments in code to figure out the current progress, a printscreen is also provided below.
How can I get the QUERY to echo out the SUM based on the $year $month and $day_num on each row in the calendar?
This is my current code:
<?php
include 'db.php';
// GET USER DATA
$sql = "SELECT * FROM users where user_username = '".$_SESSION['username']."'";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) {
$loginuser_id = $_GET['id'];
}
// GET TIME TRACKING DATA
$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 = '2018-09-15'
";
$result = $mysqli->query( $query ) or die($mysqli->error);
$row = $result->fetch_assoc();
$user_hours = $row['myhours'];
// VIEW THE SUM OF HOURS BASED ON THE DAY FROM THE QUERY ABOVE
echo $user_hours;
// THE CALENDAR
showmonth($m = $_GET['m'] ,$y = $_GET['y']);
function showmonth($month, $year) {
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
$days_in_month = cal_days_in_month(0, $month, $year);
echo "<div id='container'>";
echo "<div id='datenav'>";
echo "<div id='prevbtn'>prev</div>";
echo "<div id='date'>$title $year</div>";
echo "<div id='nextbtn'>next</div>";
echo "</div>";
echo "<div id='cal'>";
$day_num = 1;
while ( $day_num <= $days_in_month ) {
$cnt = 0;
while ($cnt < 7) {
echo "<div id='days'>";
echo "<h4>$day_num</h4>";
echo "<h5>40h</h5>"; // HERE I WANT THE SUM OF TIME TRACKING BY EACH DAY
echo "</div>";
$day_num++;
if ($day_num > $days_in_month) {break;}
}
}
echo "</div>";
echo "</div>";
} // end of function
?
The current output: (146 is the SUM of all the hours on the specified date in the QUERY, and all the 40h in the picture are manually written. That's the place that I want to echo the QUERY output for each day)
The desired output would be to have 146 on the day it's summarized from listed in the calendar, and the same for each and every other day but with those days hours.
Big thanks in advance and let me know if you need any more data!
Sample data:
DB: hours
hours_id userhours_id projecthours_id user_hours hours_timeoccur
1 5 12377 5 2018-08-14
2 3 12378 100 2018-09-15
3 3 12378 46 2018-09-15
4 1 12378 5 2018-09-16
5 5 12379 5 2018-09-17
DB: users
user_id user_username .....
1 Adam
2 Bryan
3 Clinton
4 David
5 Eric
DB: projects
project_id project_name .....
1 My first test
2 My second test
3 My third test
4 My fourth test
5 My fifth test
Figured it out!
I moved the QUERY inside the calendar code and then it all worked out just perfect!
Related
In my database I have a table with consecutive dates. I group all dates to calendar weeks and display them in a table in my CSS.
Now i want to display also the first and the last day of the calendar week in my table.
Here is my code:
$statement = $pdo->prepare("SELECT WEEK(Date), Date FROM table GROUP BY WEEK(Date)");
$result = $statement->execute();
$count = 1;
while($row = $statement->fetch()) {
echo "<tr>";
echo "<td>".$row['WEEK(Date)']."</td>";
echo "<td>".$row['Date']."</td>";
Currently there is only shown the first day of the calendar week.
I thought of duplicating $row['Date']and adding +6 (days) but the first dataset is not the first day of the week. So the first date range of the first calendar week would be wrong.
Does anyone has an idea how to solve this problem?
See a test of this here: https://www.tehplayground.com/c2dOYxxwIa9LDscW
while($row = $statement->fetch()) {
$thedate = $row['Date'];
$dow = date('w', strtotime($thedate)); // the day of the week of the date - number based - zero is sunday
$dowsun = date('Y-m-d', (strtotime($thedate) - ($dow * 60*60*24))); // first day of that week (sunday)
$dowsat = date('Y-m-d', strtotime($thedate) + ((6-$dow) * 60*60*24)); // last day of that week (saturday)
/* for example:
if $row['WEEK(Date)'] = "2021-06-08" which is a Tuesday
$dowsun = "2021-06-06" which is a Sunday
$dowsat = "2021-06-12" which is a Saturday
with this answer you get the full week that contains the target date
*/
echo "<tr>";
echo "<td>".$row['WEEK(Date)']."</td>";
echo "<td>".$row['Date']."</td>";
I'm starting to play with PHP and MySQL.
However, I have encountered a problem and can not deal with it.
In my database I have data such as: id, domain_name, year, month, organic_search_visit
I want to display them now using PHP, but the data is displayed not as I want.
The data in my database looks like this:
Site1.com
January - 2017 - 11k visit
February - 2017 - 10k visit
...
January - 2018 - 11k visit
February - 2018 - 10k visit
...
Site2.com
January - 2017 - 11k visit
February - 2017 - 10k visit
...
January - 2018 - 11k visit
February - 2018 - 10k visit
...
I want to display the data from the database using PHP in the table so that it can see how I had traffic in a given year / month.
My code:
$results = $mysqli->query("SELECT domain_name, month, year, organic_search_visit FROM organic_search");
echo "<table>";
echo "<thead><tr>";
while($row = mysqli_fetch_array($results))
{
echo "<td>Month</td>";
echo "<th>".''.$row['month'].''."</th>";
echo "</tr><thead>";
echo "<tr>";
echo "<td>2017</td>";
echo "<td>".''.$row['organic_search_visit'].''."</td>";
}
echo "</tr>";
echo "</table>";
Start with sorted result data. Iterate through the data and print the relevant table and row tags when they change in the data.
You can use something like the example below. Note that this has a fixed list of months for column headers and requires the data to match.
$results = $mysqli->query("SELECT domain_name, month, year, organic_search_visit FROM organic_search GROUP BY domain_name,month,year, ORDER BY domain_name, year, month");
$months = array('January', 'February', 'March'); //etc
$domain = "";
$year = "";
$month = 0;
while($row = mysqli_fetch_array($results))
{
if($domain!=$row['domain_name']){ //Check if the domain has changed
$domain = $row['domain_name'];
$year = "";
$month = 0;
if($domain!="") echo "</tr></table>"; //Only close last domain table if there was a previous domain
echo "<table><tr><th>".$domain."</th>"; //Start new domain table
foreach($months as $month) echo "<td>".$month."</td>"; //Print header row
echo "</tr>";
}
if($year!=$row['year']){
$year = $row['year'];
$month = 0;
if($year!="") echo "</tr>";
echo "<tr><td>".$year."</td>";
}
while($months[$month] != $row['month']){
$month+=1;
echo "<td></td>";
}
echo "<td>".$row['organic_search_visit']."</td>";
}
echo "</tr></table>";
I have problem getting the year
I have here in my database year of 11/11/2016 - 11/13/2015 - 11/13/2014
the problem is I want to get that year just deducting current year
so if the current year is 2017 I want to get 2016,2015,2014 , and the next year
2018 the year will get is 2017,2016,2015
$selectmemberpaid = mysql_query("SELECT
memberid,StartDate,EndDate,tblpayments.activityname,amount_paid FROM
tblattend_activity LEFT JOIN tblpayments ON tblattend_activity.memberid =
.profile_id WHERE memberid='201400001'");
That is my query..
and this is my while loop
while ($rows = mysql_fetch_assoc($selectmemberpaid)) {
# code...
$dateTimestamp = strtotime($rows['StartDate']);
$year = date("Y", $dateTimestamp);
}
You can get the year from a date string by doing something like this:
$dateTimestamp = strtotime('2015-12-12');
$year = date("Y", $dateTimestamp);
echo $year;
Once you get it you can add or subtract to get the previous/next year (after converting the value into an integer).
I'm not sure I understand what you're asking but:
Query!
$rest = substr(date('d-m-Y'), 6);
$rep = $idz->query("SELECT * FROM stack1 where SUBSTR(date_depart, 6) < $rest
order by SUBSTR(date_depart,6) desc limit 3");
while ($row = $rep->fetch())
{
echo $row['date_depart'];
//echo "<br>";
}
$rep->closeCursor();
Result
This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 5 years ago.
I have a table in MySQL with a date field (called NDate) which contains standard date values ("2017-04-17","2017-04-18", etc.).
Through PHP webpage, I am trying to take the system date (say today is 2017-04-17), and then pull all rows from the above table where NDate="2017-04-17". No issues till here.
I have a requirement to increment the day (starting today and going on for next 10 days - i.e. 2017-04-17 to 2017-04-26), and for each day report entries under a different heading like "Entries for 2017-04-17" which will list all rows having NDate 2017-04-17, "Entries for 2017-04-18" which will list all rows having NDate 2017-04-18.
I was trying to use a for loop with PHP date_modify function to increment the days one by one, but it is not showing any results.
Here are the selected pieces of code:
date_default_timezone_set('US/Eastern');
$datev = date("Y-m-d");
for ($x = 0; $x <= 10; $x++)
{
$datev=date_modify($date,"+$x days");
echo "before date format<br>"; // echo statement 1
echo "date is: $datev <br>"; // echo statement 2
$sql = "SELECT * FROM tablename where Ndate='$datev'";
echo "before result<br>"; // echo statement 3
...
...
...
}
Output on webpage shows only statement 1. But echo stats 2 and 3 are not printed.
You can increment days using strtotime function as a parameter to date function.
For 10 days, you can use for loop, to build an array of days. Then iterate over it, to execute queries you need.
$today = date('Y-m-d');
$dates=array($today);
for($i=1;$i<10;$i++) {
$NewDate=date('Y-m-d', strtotime("+".$i." days"));
$dates[]=$NewDate;
}
foreach($dates as $dt) {
// sql stuff here
echo "date is: $dt <br>";
$sql = "SELECT * FROM tablename where Ndate='$dt'";
echo "before result<br>";
// .....
}
This code should work for your case. If any problems, just let me know.
Try this:
$start = strtotime(date('Y-m-d'));
$end = strtotime(date('Y-m-d', strtotime('+10 days')));
while($start <= $end)
{
$date = date('Y-m-d', $start);
//use $date to do stuff
//SELECT * FROM tablename where Ndate='$date'
$start = strtotime("+1 day", $start);
}
I have a script that counts how many days have past since the start date stored in my database.
The theory is, a user signs up and has 7 days to complete registration, from the admin side of things, this script is used to monitor where a user is upto and how many days have past since they registered.
So in this example we give the user 7 days grace in which to complete. This echos out like '(number of days out of 7 have past'
Then after 7 days this should change to give the number of days overdue. so if a user has 7 days to complete and they take 8 days then this will take into account the first 7 days grace and then have an overdue date of 1 day.
so for instance it should echo out '1 day overdue', rather than what it is currently doing and saying 8 days overdue, i am trying to do this by minusing the first 7 days.
i have been told this will help however i am having trouble getting it to work with my date variable
$pre_date=date('Y-m-d', strtotime('-7 days'));
heres my compelte code, please can someone show me where i am going wrong
<?php include 'config.php';
$data = mysql_query("SELECT *,
TIMESTAMPDIFF(DAY, date, CURDATE()) AS expire_date
FROM supplier_session
ORDER BY expire_date ASC")
or die(mysql_error());
echo "<table class=\"table\" style=\"width:995px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px;\" >
<tr>
<td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Application Started:</td><td style=\"width:200px;\">Application Duration:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";
while($row = mysql_fetch_array( $data )) {
$days = $row['expire_date'];
$when = $days*0;
$str = $row['expire_date'];
$str2 = substr($str, 0); // "quick brown fox jumps over the lazy dog."
$pre_date=date('Y-m-d', strtotime('-7 days'));
if ($when <= 31){
echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>";
echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>";
echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
echo "<td>"; echo date('d/m/Y',strtotime($row['date'])); echo "</td>";
if ($days >= 8) {
echo "<td style=\"width:200px;\"><p>{$pre_date} days overdue</td>";
}
elseif ($when <= 7){
echo "<td style=\"width:200px;\"><p>{$str2} of 7 days past</td>";
}
}
echo "<tr>";
}
echo "</table>"; //Close the table in HTML
?>
You've forgotten to pass the date variable to strtotime, so you are always substracting 7 days to the actual date and getting 8 as a result.
Try with a subraction of the actual date minus the expire date in order to get the number of days that have past.
I think u have some mistake in ur function
$pre_date=date('Y-m-d', strtotime('-7 days'));
For example u can try to use somth like that
$datetime = date("Y-m-d", strtotime( date( 'Y-m-d' )." +2 days"));
Result of function looks like:
2014-05-08
Also there more information what u need:
Click.
With unixtime looks like that. We take difference between 2 dates in second and divide it on number of seconds in day
<?php
date_default_timezone_set('UTC');
$now_time = time();
$date_before = time()-(7*24*60*60);// 7 days; 24 hours; 60 mins; 60secs
echo($now_time);
echo('<br>');
echo($date_before);
echo('<br>');
$difference = $now_time - $date_before;
$result = intval($difference/(24*60*60)); //seconds in day
echo($difference);
echo('<br>');
echo($result);
also u can get time from date with:
$unixtime = strtotime('22-09-2008');