I have in PHP:
<?php
$columns = array('mon','thu','wed','tue', 'fri', 'sat', 'sun');
$num_cols = count($columns);
$col = 0;
$datetime = new DateTime("06:00");
echo "<table>"; // border=\"1\" for visible border
// Day header
echo "<tr>";
foreach($columns as $col) {
echo "<td at='$col'>$col</td>";
}
echo "</tr>";
// Times
for($i=0; $i<20; $i++) { // FIXME for more entries or other step width
echo "<tr>";
$datetime->modify('+30 minutes');
for ($j=0; $j<$num_cols; $j++)
echo '<td class="' . $datetime->format('H:i') . '">' . $datetime->format('H:i') . '</td>';
echo "</tr>";
}
echo "</table>";
?>
and in database MySQL:
id | day | hours
1 | mon | 07:00
2 | thu | 08:40
3 | wed | 12:30
4 | thu | 13:00
5 | fri | 05:00
etc
how is the best method for checking this? If date is in database then i would like make TD with red background, for example add class red:
echo "<td at='$col' class='red'>$col</td>";
Check this in each loop FOR is not good idea - this generated to many query for database.
How can i make it?
I can use also jquery and for example JSON if this is possible.
List them all in a keyed array, then when looping through the calendar simply check for these keys.
For example, you'd get the array array(1 => array('07:00' => true)) (day of the week => appointments).
You can then check isset($array[$datetime->format('N')][$datetime->format('H:i')])
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
How to find out missing date in MySQL using PHP?
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
currently result
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
and i want result like this
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
2015-07-04
2015-07-05
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
2015-07-11
2015-07-12
Ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
All of your missing check ins are not assigned to any user. If you want to fetch records assigned to user ali or unassigned, between gives dates:
SELECT * FROM attend WHERE (user_name='ali' OR user_name = '') AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date
It would be much cleaner if you show us your schema.
I'd use timestamp for that date column. If you do this, you can simply "check" which day is expected to come next. If its not that day, print that day like you wanted and then continue, if you got that expected date.
Do do so, you should save all your timestamps at a given time of that day. Like always 12:00:00. This makes handling those numbers much easier.
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$iLastTimestamp = 0
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
if ($iLastTimestamp != 0)
{
while ($iLastTimestamp != $row['date'])
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td> </td>';
echo '<td>' . date("Y-m-d", $iLastTimestamp) . '</td>';
echo '<td> </td>';
echo '<td> </td>';
echo "</tr>";
$iLastTimestamp = strtotime("+1 Day 12:00:00", $iLastTimestamp)
}
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
Another way is to convert your date into an timestamp first, before using the code I posted. You can use the "strtotime"-Func here, too. Just add the time, just to be sure. :)
You can do this with a range in a function, this will permit you to reuse your code:
function getAttendInInterval($begin,$end) {
$begin = new DateTime( $begin );
$end = new DateTime( $end );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ) {
$result = mysql_query('SELECT * FROM attend WHERE user_name="ali" AND date = '".$dt->format( 'Y-m-d' )."' order by date');
...
}
This fonction permits to show the attend for each day in an interval.
and then:
getAttendInInterval('2015-07-01','2015-07-15');
you can use date in LOOP and if date is match in database then show all info. of database with date otherwise show only date.
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>';
}
In the database I have various dates stored like so:
ID| Dates
-----------------
1 | 23 April 2014
2 | 24 April 2014
3 | 25 April 2014
4 | 01 May 2014
5 | 02 May 2014
I managed to trim all the digits from the string with PHP which only leaves me with the month titles.
What I am trying to accomplish is to loop the months with a While loop and only show each month only once.
I want to achieve the following:
<h1>April<h1>
<p>23 April 2014</p>
<p>24 April 2014</p>
<p>25 April 2014</p>
<h1>May</h1>
<p>01 May 2014</p>
<p>02 May 2014</p>
So, what I actually want is to dynamically sort the months under its rightful tag
I hope somebody can help
Thank you
If your are query ascending by date, you can achieve your requirement by following example:
<?php
$results = array();// your dataset
$month_name = array(); // empty array
foreach($results as $row){
$this_month = date('M', strtotime($row['date_field']));
if (!in_array($this_month, $month_name)) {
$month_name[] = $this_month;
echo "<h1>{$this_month}</h1>";
}
echo "<p>{$row['date_field']}</p>";
}
I believe there are more better solution can be possible (by change database data field as date instead of keep date as string), but from your current scenario, this solution I think perfect.
******EDIT******
Updated code base on your provided example:
$month_name = array();
$result = mysql_query($sql);
while ($record = mysql_fetch_assoc($result)) {
// $record['date_field'] is your dates field of your database
$this_month = date('M', strtotime($record['date_field']));
if (!in_array($this_month, $month_name)) {
$month_name[] = $this_month;
echo "<h1>{$this_month}</h1>";
}
echo "<li> <a href='dates.php?ID=" . $record['ID'] . "'>";
echo "<p><strong>" . $record['date'] . " | " . $record['timeStart'] . " - " . $record['timeEnd'] . "</strong></p>";
echo "<p>" . $record['place'] . " - " . $record['provincie'] . "</p>";
echo "<p>" . $record['kind'] . "</p>";
echo "</a> </li>";
}
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>