What I need is get listed next month days, every new day on new line.
Actually I want it look next:
date | username1 | username2
_________|___________|__________
| |
Mon - 01 | user5 | user2
| |
Tue - 02 | user3 | user2
....
and so on
Later i will be collect these dates and usernames to and update sql table (same type, like example here).
I have this code:
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
//loop through all days
for ($i = 1; $i <= $day_count; $i++) {
$date = $year.'-'.$month.'-'.$i; //format date
$workdays[] = $i;
and with foreach echo i get days 1, 2, 3 ... and so on correctly, everyone on new line. But if i place inside foreach this:
<?php foreach($workdays as $value['date']): ?>
<tr>
<td><?php if ($value['date'] != 'dd'){
setlocale(LC_TIME, 'fi_FI.UTF-8');
echo ucfirst(strftime("%a - %d", strtotime($value['date'])));
} else {
echo 'wrong';
}?></td>
<td> </td>
</tr>
<?php endforeach ?>
I get on every new line Mon - 01.
What is wrong (after that, that i newbie on php)?
Try something like this.
<?php
$day_count = date('t',strtotime('+1 month'));
$month = date('m');
$year = date('Y');
if ($month == 12) { $month = 1; $year++; }
for($i = 1; $i<=$day_count; $i++) {
echo '<tr>';
echo '<td>'.date('D',strtotime("$month/$i/$year")).' '.$i."</td>";
echo '<td> </td>';
echo '</tr>';
}
Related
I have look at 100 calendar code and thkink there is no good one. So i came up with my own code, but need som help to understans what i am missing. Thinking now that i mixed up the loops or somthing have try this for a while but cant get it right. The result i getting now is in a line from top to bottom. The dont seem to work. Can somone help me understand what i need to do?
<table border="1">
<?php
//counter array
$counter = 0;
//start days array
$list=array();
//current day
$curDay = date("28", $today);
// mktime(0, 0, 0, date("m"), date("d") , date("Y"));
$today = mktime(0, 0, 0, date("m"), '28', date("Y"));
// curent month
$curentmonth = '09';
// curent year
$year = 2018;
// curent month
$month = date('m');
//loop number of days in month and list them in the array
for($d=1; $d<=31; $d++)
{
$datetime=mktime(12, 0, 0, $curentmonth, $d, $year);
if (date('m', $datetime)==$curentmonth)
$list[]=date('d', $datetime);
//try to get the right 7 weeks in a month start monday
for ($day = 1; $day <=7; $day++) {
$datetime = strtotime($year.'W'.$month.$day);
echo "<tr>";
}
// make this day red
if ($curentmonth === $month && $list[$counter] === $curDay) {
echo "<td bgcolor='ff0000'>$list[$counter]</td>";
$counter++;
// all other days in the month
} elseif ($month === $curentmonth) {
echo "<td>$list[$counter]</td>";
$counter++;
// other month
} else {
echo "<td> </td>";
echo "</tr>";
}
}
?>
I've tried coercing your code into doing what you want, but I'm afraid there are so many logical problems that it's better to just go back to the drawing board entirely.
Looking at your code, it seems what you want to accomplish is a table that displays the weeks of the current month below each other, with each week starting on monday. For table cells that don't contain a date (e.g. the month starts or ends in the middle of a week) you want to display only a space in the cell so that it is empty.
Additionally, you want to highlight the current date.
So essentially, your intended end result is something like this for today (September 30th, 2018):
+----+----+----+----+----+----+------+
| | | | | | 1 | 2 |
+----+----+----+----+----+----+------+
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+----+----+----+----+----+----+------+
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+----+----+----+----+----+----+------+
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
+----+----+----+----+----+----+------+
| 24 | 25 | 26 | 27 | 28 | 29 | *30* |
+----+----+----+----+----+----+------+
There are many ways to accomplish this goal. Here's one way of doing it with simple date manipulation, so that there's no need for arrays and multiple loops:
// the month to render and day to highlight
$today = new DateTime();
// we'll need these to check if we're at the day we need to highlight
// or if we're rendering a date that's outside $today's month
$currentDay = $today->format('j');
$currentMonth = $today->format('n');
$daysInCurrentMonth = $today->format('t');
// figure out what Monday needs to kick off our table
$date = (clone $today)->modify('first day of this month');
if ($date->format('w') != 1) {
$date->modify('previous monday');
}
$shouldStopRendering = false;
echo '<table border="1">';
while (!$shouldStopRendering) {
$weekDay = $date->format('w');
$month = $date->format('n');
$day = $date->format('j');
// start a new table row every time we hit a Monday, note that
// since we forced $date to be a Monday above, our table should
// now always start with a <tr>
if ($weekDay == 1) {
echo '<tr>';
}
if ($month != $currentMonth) {
// we're either at the beginning or end of our table
echo '<td> </td>';
} else if ($day == $currentDay) {
// highlight the current date
echo '<td bgcolor="#ff0000">' . $day . '</td>';
} else {
echo '<td>' . $day . '</td>';
}
if ($weekDay == 0) {
// every time we hit a Sunday, close the current table row
echo '</tr>';
// on a Sunday, if we've gone outside the current month **or**
// this Sunday happens to be the last day we need to render,
// stop looping so we can close the table
if ($month != $currentMonth || $day == $daysInCurrentMonth) {
$shouldStopRendering = true;
}
}
// move on to the next day we need to display
$date->modify('+1 day');
}
echo '</table>';
Note this line:
$date = (clone $today)->modify('first day of this month');
The reason I'm cloning $today instead of just creating a new DateTime instance, is that you may wish to change $today to render a different month. Or maybe you're looping over a number of months to render a calendar for a whole year. I don't know which is the case, so I'm basing $date off of whatever $today happens to be.
Don't just do this:
$date = $today->modify('first day of this month');
That'll work, but it will also modify $today which may not be what you want if you want to re-use the current date after rendering the table.
If you're using an older version of PHP, you may not be allowed to do (clone $today)->modify in a single line. If that's the case, just split it up into two lines:
$date = clone $today;
$date->modify('first day of this month');
Here's a demo of this code in action (with some added whitespace for readability of the generated HTML): https://3v4l.org/Z89i8
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!
I want to create a list of weekday dates with name of the weekday.
If today is monday - 2017-01-02 (Y-m-d), then I want the list to be something like this:
Mon-02 | Tue-03 | Wed-04 | Thu-05 | Fri-06 | Sat-07 | Sun-08
My code below will give such result, if today is monday.
This is what I have so far.
function NextDayDate($day) {
return new DateTime('next ' . $day);
}
$dt = new DateTime();
$today=date("l");
if ($today=="Monday") {
echo $dt->format('d'); // Todays date
echo '</br>';
}
elseif ($today!="Monday"){
echo '</br>';
$nextMonday = NextDayDate('Monday');
echo $nextMonday->format('d'); // Next date
echo '</br>';
}
if ($today=="Tuesday") {
echo $dt->format('d'); // Todays date
echo '</br>';
}
elseif ($today!="Tuesday"){
echo '</br>';
$nextMonday = NextDayDate('Tuesday');
echo $nextMonday->format('d'); // Next date
echo '</br>';
}
If today is Tuesday, then for monday I will have Mon-09 because that is the next date for monday.
Mon-09 | Tue-03 | Wed-04 | Thu-05 | Fri-06 | Sat-07 | Sun-08
I want to keep last days date so the list does not change.
Maybe there is a way to get current weeks day names with date?
It is much more easy:
$a = new DateTime();
$oneDay = new DateInterval("P1D");
// Here you add one day to your date until it will Monday
while ($a->format('D') != 'Mon') {
$a->add($oneDay);
}
// Here you print 7 days from your target Monday
for ($i = 0; $i < 7; $i++) {
echo $a->format("D-d");
$a->add($oneDay);
}
My for loop is true when variables are equal.
Why is that when i'm only using "smaller than"?
I have a while loop that prints dates from mysql.
$today_nr is a day nr stored in db. ex: 02
Now i´d like a for loop to print out dates that is missing.
ex: 01, 02, 03. If first reccord in mysql is 04.
So i use this for loop. But it will echo both "01" from mysql($today_nr) and "1" from $i
PHP
$today_nr = $date->format('d');
$i = 1;
for(;$i < $today_nr; $i++){
echo $i;
}
FULL SCRIPT
https://eval.in/367692
Line 532
The Print for loop is printing 1 after while loop printed 01.
01 Ons 04:41 13:10 0.50 7.97 -0.03 1.32
1
02 Tor 04:40 13:18 0.50 8.13 0.13 1.33
2
3
4
5
6
07 Tis 04:41 12:58 0.50 7.77 -0.23 1.32
7
08 Ons 04:43 13:08 0.50 7.92 -0.08 1.28
It isn't like what you are saying in my case the code below works perfect with not an issue as you provided and
It works as you expects:
<?php
$today_date = date('d', time());
// $today_nr = $date->format('d');
$i = 1;
for(;$i < $today_date; $i++){
echo $i; // result: 12345678910111213141516171819
}
I solved it like this:
PHP
//Loop out dates missing in db
//If $i is bigger than today nr
if($i > $today_nr){
$i = $today_nr;
}
//Loop out missing days
for(;$today_nr != $i;){
//Set data about day
$iDate = $year."-".$selected_month."-".$i;
$iDate = new DateTime($iDate);
$iDay = $iDate->format('D');
//Translate iDay. Eng - Swe
include('../../../include/translate_days_iDay.php');
//Echo out table
echo "<tr>";
echo "<td class='print_table_text'>".$iDate->format('W')."</td>";
echo "<td class='print_table_text'>".$iDate->format('d')." ".$iDay."</td>";
echo "</tr>";
//Add to i
$i++;
//if i is bigger than a month
if($i > $days_in_month){
break;
}
}
//If today and i is equal
if($today_nr == $i){
$i++;
}
I am creating a table for a schedule and I am having trouble formatting it correctly. Here is what my output table looks like
Users| Action | Monday | Monday | Monday | Wednesday
09/23/13 09/23/13 09/23/13 09/25/13
11:00 AM 1:00 PM 2:00 PM 10:00 AM
----- -------- ---------- --------- ----------- -----------
Jack
John
Tim
I want my output to look like this:
Users| Action | Monday | Monday | Monday | Wednesday
09/23/13 09/23/13 09/23/13 09/25/13
11:00 AM 1:00 PM 2:00 PM 10:00 AM
----- -------- ---------- ----------- ------------- ---------
Jack
John * * *
Tim *
new
This is reading data from two text files, one for the schedule that looks like this:
2013-09-23^11:00|13:00|14:00
Where it is visualized to look like this with an index for the users file
0 1 2
2013-09-23^11:00|13:00|14:00
Where each number maps to a user
eg user file
Jack
John^0|1|3
Tim^2
John's 0 maps to the first date of the schedule file and marks it..
heres my code do you have any idea of how to fix it? i need to first put blank rows in per user based on the columns that the schedule file reads in...
<!DOCTYPE html>
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<h2>
<form action = "update.php" method = "POST" >
<center>Select Your Meeting Times</center></h2>
<?php
echo "<table border= '1'
cellpadding='10'>
<tr>
<th>User</th>
<th>Action</th>";
date_default_timezone_set('America/New_York');
//error_reporting(1);
getTimes();
displaySchedule();
function displaySchedule()
{
// used for displaying schedule times
$text = "user.txt"; // open up the user file.
$f = fopen($text, "r+");
if (file_exists("user.txt"))
{
while($line = fgets($f,1000))
{
$name = getUsers($text, $line);
echo "<tr>" . "<td>" . $name . "</td>" . "</tr>\n";
}
}else{
#create the file method.
}
echo "<tr>" . "</tr>";
}
#this method gets the users from the text file and diplays them.
function getUsers(&$text1, &$line)
{
list($name, $num) = explode('^', $line);
$num1 = explode('|', $num); // num 1 now holds the number where the time entry mark goes
// setTimes($name, $num1) // sets the times for the user.
return $name;
}
#When the user is either new or active, set the times
function setTimes(&$name, &$num1)
{
}
function getTimes()
{
$file = file("schedule.txt"); // open up the schedule file
// loop through the schedule file
foreach($file as $s){
# s = string like '2013-04-11^12:00|4:00|14:00'
list($year, $rest) = explode("^", $s);
$rest_arr = explode("|", $rest); // time = 12:00 etc..
list($year, $month, $day) = explode('-', $year); // this cuts them down.
$year= intval($year, 10);
$month= intval($month, 10);
$day= intval($day, 10); $h = mktime(0, 0, 0, $month, $day,$year);
$d = date("F dS, Y", $h); //used to get the day of the week
$w= date("l", $h); // w now holds the day of the week.
// while through the schedule file, loop through each of times and displays them.
foreach($rest_arr as $time){
//$convert = (string)$rest_arr;
//$convertedTime = date("g:ia", strtotime($convert));
echo "<th>" . $w . "<br>" . $month . "/" . $day . "/" . $year . "<br>" . $time . "</th>\n";
// sets the header
} // end this
} // end 1st foreach for file.
}
function createFile()
{
}
function drawTable()
{
$rows = 10; // define number of rows
$cols = 4;// define number of columns
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
}
echo "<th><br></th>
<tr>
<th>Total</th>
</tr>
</table>";
?>
</body>
</html>