Something about php calendar - php

I am a new learner of PHP, now I want to use it to do a calendar like the following. There are something that I don't know.
How to get the first day of the month is sunday or another?
How to output all the days of the month and emphasize today?
The code:
<table cellspacing="0" cellpadding="3" bordercolor="#000000" border="1" style="width: 105px; border-collapse: collapse;">
<tbody>
<tr>
<td colspan="7"><?php echo date('F')." ".date('Y');?></td>
</tr>
<tr>
<td>Su</td>
<td>M</td>
<td>Tu</td>
<td>W</td>
<td>Th</td>
<td>F</td>
<td>Sa</td>
</tr>
<?php
$numrows = ceil(date('t')/7);
for($k=1;$k<=$numrows;$k++){
?>
<tr><td></td></tr>
<?php }?>
</tbody>
</table>
I don't know how to output the following days.

First Day of Month
date('d', strtotime('2012-04-01'));
Replace year and month with actual year and month you want to check. Leave the 01 for the first day.
http://php.net/manual/en/function.date.php
Output Days of Month and Emphasize Today
You can use date() with no second parameter to get the current time formatted however you want. To output the days of the month, you'd need to know how you wanted them formatted, so that's up to you to figure out. But, to get the number of days in a given month, you can use:
date('t')
That will return a number between 28 and 31, depending on how long the month is.
You really should read this: http://www.php.net/manual/en/ref.datetime.php

You never need these things nowadays when http://jqueryui.com/demos/datepicker/ is available, but if you want here is the code I wrote about 10 years ago, lol :)
<?
function my_calendar($fill=array()) {
if (isset($_GET['y'])) $y=$_GET['y'];
if (isset($_GET['m'])) $m=$_GET['m'];
if (isset($_GET['date']) AND strstr($_GET['date'],"-")) list($y,$m)=explode("-",$_GET['date']);
if (!isset($y) OR $y < 1970 OR $y > 2037) $y=date("Y");
if (!isset($m) OR $m < 1 OR $m > 12) $m=date("m");
$month_stamp=mktime(0,0,0,$m,1,$y);
$month_name=date("M",$month_stamp);
$day_count=date("t",$month_stamp);
$weekday=date("w",$month_stamp);
if ($weekday==0) $weekday=7;
$start=-($weekday-2);
$last=($day_count+$weekday-1) % 7;
if ($last==0) $end=$day_count; else $end=$day_count+7-$last;
$today=date("Y-m-d");
$prev=date('?\m=m&\y=Y',mktime (0,0,0,$m-1,1,$y));
$next=date('?\m=m&\y=Y',mktime (0,0,0,$m+1,1,$y));
$i=0;
?>
<table border=1 cellspacing=0 cellpadding=2>
<tr>
<td colspan=7>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr>
<td align="left"><<<</td>
<td align="center"><? echo $month_name," ",$y ?></td>
<td align="right">>>></td>
</tr>
</table>
</td>
</tr>
<tr><td>Mon</td><td>Tue</td><td>Wed</td><td>Th</td><td>Fri</td><td>Sat</td><td>Sun</td><tr>
<?
for($d=$start;$d<=$end;$d++) {
if (!($i++ % 7)) echo " <tr>\n";
echo ' <td align="center">';
if ($d < 1 OR $d > $day_count) {
echo "&nbsp";
} else {
$now="$y-$m-".sprintf("%02d",$d);
if (is_array($fill) AND in_array($now,$fill)) {
echo '<b>'.$d.'</b>';
} else {
echo $d;
}
}
echo "</td>\n";
if (!($i % 7)) echo " </tr>\n";
}
?>
</table>
<? } ?>
Use example
<?
if (isset($_GET['date'])) echo "Date picked: ".$_GET['date'];
my_calendar(array(date("Y-m-d")));
?>
it displays Monday first though. You need to remove some lines to get it to Sunday

Related

How to compare days with current days in php?

<table class="table row-border order-column">
<thead>
<tr>
<th></th>
<?php
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$days;$i++)
{
?>
<th><?php echo $i; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $key => $value)
{
?>
<tr>
<td class="cap"><?php echo $value->name; ?></td>
<?php
for($i=1;$i<=$days;$i++)
{
$result = $this->db->select('*')->from('attendance')->where('studentID',$value->studentID)->where('day(date)', date($i))->where('month(date)', date($month))->where('Year(date)', date($year))->get()->row();
if(empty($result))
{
$date = date('Y-m-d');
$current_date = date('d',strtotime($date));
if($i < $current_date)
{
?>
<td class="absent">A</td>
<?php
}
else
{
?>
<td></td>
<?php
}
}
else
{
$dateday = date('d',strtotime($result->date));
if($i == $dateday)
{
?>
<td class="present">P</td>
<?php
}
}
}
?>
</tr>
<?php
}
?>
</tbody>
In the above code I want to show absent while compare $current_date and $days but now what happen here only current month date show absent and previous month date show blank. Now, What am I going to do I don't want to show absent in future date. So, How can I do this? Please help me.
Thank You
Use Unix timestamp for your calculation and after that change Unix to gmt or utc time using ready functions to show.

Change date from xml to more readable content using php? [duplicate]

This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 3 years ago.
XML is outputting time like this:
<time from="2020-02-03T12:00:00" to="2020-02-03T18:00:00" period="2"
^ this is dynamic and changes all the time ^
I would want to display it like this:
Feb 12:00 - 18:00
This is my PHP:
$url = ('https://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml');
$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
array_push($result, [ "from" => (string)$content['from'],
"to" => (string)$content['to'],
'symbol' => (string)$content->symbol['name'],
'temperature' => (string)$content->temperature['value'],
'windDirection' => (string)$content->windDirection['code'],
'windSpeed' => (string)$content->windSpeed['mps'],
]);
}
This is my html:
<section>
<div class="tbl-header">
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Time</th>
<th>Weather</th>
<th>Temperature</th>
<th>Wind</th>
</tr>
</thead>
</table>
</div>
<div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<?php foreach ($result as $value) { ?>
<tr>
<td>Bergen <br /><?php echo $value['from'] ?> til <?php echo $value['to'] ?></td>
<td><?php echo $value['symbol'] ?></td>
<td><?php echo $value['temperature'] ?> °C</td>
<td><?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</section>
how can I do that the best way by using php? Any help is greatly appreciated.
UPDATE : Simple solution With php self.
<td>Bergen <br /><?php echo date("j M, g:i", strtotime($value['from'])); ?> - <?php echo date("g:i", strtotime($value['to'])); ?></td>
Result : 3 Feb, 2:00 - 6:00
More examples : https://www.php.net/manual/en/function.date.php
With custom function
This function shortens months names
function shorten_text($text, $max){
$text = substr($text, 0, $max);
$text_son = strrchr($text, " ");
$text = str_replace($text_son,"", $text);
return $text;
}
You need to format date like this
<td>Bergen <br /><?php echo date('d', strtotime($value['from'])).".". shorten_text(date("F", strtotime($value['from'])), 3) ." ". date('H:i', strtotime($value['from']));?> til <?php echo date('d', strtotime($value['to'])).".". shorten_text(date("F", strtotime($value['to'])), 3) ." ". date('H:i', strtotime($value['to']));?></td>
This will give you result like 03.Feb 13:00 til 03.Feb 18:00. play with it and turn as you like.

How to show calendar month's previous days?

I'm trying to make a calendar that shows this month, and then fills in the remaining days on the end rows with the dates from the next and previous months.
I managed to make it show next months dates and the thing is the issue is that I need it to show previous months dates as well.
If someone manages to figure out the issues please add what the problem was as well how you managed to fix it, to help me in the future.
I will add a image of the calendar as well.
The script:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
$week_day_first = date('N', mktime(0, 0, 0, date('m'), 1, date('Y')));
// Get previous month dates
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}else{
echo '<span class="given-date">'.$d.'</span>';
}
}else{
//Here comes previous dates
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
When you calculated $week_day_first, you can save your time calculated from the first day of the month into a variable.
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
Then you can reuse that later to calculate the offset of days before the first of the month.
date('d', strtotime("$offset day",$time_first_day_of_month))
Putting it all together:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
$week_day_first = date('N', $time_first_day_of_month);
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
// next month's dates
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}
// today
else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}
// this month's dates
else{
echo '<span class="given-date">'.$d.'</span>';
}
}
// last month's dates
else{
//Here comes previous dates
$offset = $d - 1;
echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
Results in this output:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<tr><td valign="top" width="14.2857%" class="disabled"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled"><span class="given-date">31</span></td><td valign="top" width="14.2857%"><span class="given-date">1</span></td><td valign="top" width="14.2857%"><span class="given-date">2</span></td><td valign="top" width="14.2857%"><span class="given-date">3</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">4</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">5</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">6</span></td><td valign="top" width="14.2857%"><span class="given-date">7</span></td><td valign="top" width="14.2857%"><span class="given-date">8</span></td><td valign="top" width="14.2857%"><span class="given-date">9</span></td><td valign="top" width="14.2857%"><span class="given-date">10</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">11</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">12</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">13</span></td><td valign="top" width="14.2857%"><span class="given-date">14</span></td><td valign="top" width="14.2857%"><span class="given-date">15</span></td><td valign="top" width="14.2857%"><span class="given-date">16</span></td><td valign="top" width="14.2857%"><div class="current-day"><span class="given-date">17</span></div></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">18</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">19</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">20</span></td><td valign="top" width="14.2857%"><span class="given-date">21</span></td><td valign="top" width="14.2857%"><span class="given-date">22</span></td><td valign="top" width="14.2857%"><span class="given-date">23</span></td><td valign="top" width="14.2857%"><span class="given-date">24</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">25</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">26</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">27</span></td><td valign="top" width="14.2857%"><span class="given-date">28</span></td><td valign="top" width="14.2857%"><span class="given-date">29</span></td><td valign="top" width="14.2857%"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled">1</td><td valign="top" width="14.2857%" class="disabled" class="week-day">2</td><td valign="top" width="14.2857%" class="disabled" class="week-day">3</td></tr></table>
Here is code using the DateTime class, which makes it quite readable:
$today = new DateTime('today'); // only change this line to test other months
$this_month = $today->format('M');
$date = clone $today;
$date->modify('first day of this month')->modify('+1 day')->modify('last Monday');
do {
echo '<tr>';
for ($weekday = 0; $weekday < 7; $weekday++){
$out = str_replace($this_month, '', $date->format('M'));
$content = "<span class='given-date'>$out {$date->format('d')}</span>";
if ($today == $date) $content = "<div class='current-day'>$content</div>";
$class = ($out ? 'disabled ' : '') . ($weekday > 4 ? 'week-day' : '');
echo "<td valign='top' width='14.2857%' class='$class'>$content</td>";
$date->modify('+1 day');
}
echo '</tr>';
} while ($date->format('M') == $this_month);
Although this is quite old now I found Jeff's solution very useful. I refactored it a little and put it in this Gist, I hope someone finds it useful. Part of it is here:
/**
* Return part of an HTML table containing all the days
* in the current month, plus padd it with next and
* previous months days if needed to fill out the grid
*
*
* #param date $date date object containing month to display
*
* #return string
*/
function get_calendar_days_in_month_html($date) {
$date_format = "Y-m-d";
$days_count = $date->format('t'); //Get number of days in this month
$weeks_count = ceil($days_count / 7); //How many weeks in this month?
$total_cells = $weeks_count * 7;
//clone is used or we literally are modifying the $date variable
$first_date_of_month = clone $date->modify('first day of this month');
$first_day_of_month = $first_date_of_month->format("N"); //returns 1-7 EG Mon-Fri
$first_date_of_grid = $first_date_of_month ->modify('-' . $first_day_of_month . ' days');
$todays_date = new DateTime();
$todays_date_str = $todays_date->format($date_format);
$selected_date_str = $date->format($date_format);
$day_of_week = 1; //FIXME: allow starting with Sunday or whatever
$ht = '<tr>';
for ($cell=1; $cell <= $total_cells ; $cell++) {
$classes = []; //CSS classes
$current_date = $first_date_of_grid->modify("+1 day");
$current_date_str = $current_date->format($date_format);
if($current_date_str == $todays_date_str) $classes[] = "today";
if($selected_date_str == $todays_date_str) $classes[] = "selected-date";
/* if current date is not from this month (EG previous or next month) then give
it a special class, you might want to grey it out or whatever */
if($date->format("m") !== $current_date->format("m")) $classes[] = "grid-filler";
$ht .= '
<td date="' . $current_date_str . '" class="' . implode(" ", $classes) . '">' . $current_date->format("j") . '</td>';
$day_of_week ++;
if($day_of_week == 8) {
$ht .= '</tr>';
if($cell < $total_cells) $ht .= '<tr>';
$day_of_week = 1;
}
}
return $ht;
}

Is it possible for PHP to specify a particular future date and time, then echo HTML?

Basically, I want PHP to refer to a specific date and then echo the HTML below into the appropriate table cell on the calendar that corresponds to that date, i.e., specify August 16, 2014 and then echo the event information. You guys are so fast, I wasn't ready for you, ha! :)
I have managed to make a calendar that only uses PHP and CSS and it seems to be working very well. Phew! I also would like to avoid Javascript and bypass the database, altogether, which I have managed to do up to this point.
I can use CSS tooltips to show events and I have them working with IOS. This is my last hurdle: is it possible for PHP to specify a particular date, say August 16, 2014 and then echo the following HTML, having it appear in the date-appropriate table cell?
<div class="has-tooltip">
Event
<span class="tooltip">Concert In The Park<br />
123 City Park Drive<br />
AnyTown, STATE 00000<br />
More Info<br />
or call 555.123.4567<br /></span>
</div>
No other users will be entering information; it's only for my personal use and I can always double-check before publishing.
The only actual hard-coded HTML that appears on my calendar page is <body></body>. Everything else is echoed by PHP, so I can't very well just add the above to an existing table cell.
Thank you for any help! :)
PHP CODE for calendar (without Tags):
$currMonth = isset($_GET['month']) ? $_GET['month'] : date('n');
$currYear = isset($_GET['year']) ? $_GET['year'] : date('Y');
$today = (($currYear == date('Y')) && ($currMonth == date('n'))) ? date('j') : 0;
$prevMonth = $currMonth==1 ? 12 : $currMonth-1;
$nextMonth = $currMonth==12? 1 : $currMonth+1;
$prevYear = $currMonth==1 ? $currYear-1 : $currYear;
$nextYear = $currMonth==12? $currYear+1 : $currYear;
$day1 = mktime(0,0,0,$currMonth,1,$currYear);
$dim = date('t', $day1);
$dayN = mktime(0,0,0,$currMonth,$dim,$currYear);
$dow1 = (date('w',$day1)+0) % 7;
$dowN = (date('w',$dayN)+0) % 7;
$calHead = date('F Y',$day1);
echo <<<EOT
<div class="calwrapper">
<div class="caltitle"><h1>Calendar</h1></div>
<div class="container">
<div class="fnl first"></div>
<div class="adjust"></div>
<div class="fnl last"></div>
</div>
<div class="caldisplay">
<table cellspacing="0">
<tr>
<td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$prevYear&month=$prevMonth"> Prev </a></td>
<td colspan="5" class="adjust">$calHead</td>
<td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$nextYear&month=$nextMonth"> Next </a></td>
</tr>
<tr>
<th class="we">Sun</th>
<th class="wd">Mon</th>
<th class="wd">Tue</th>
<th class="wd">Wed</th>
<th class="wd">Thu</th>
<th class="wd">Fri</th>
<th class="we">Sat</th>
</tr>
<tr>
EOT;
for ($d=0;$d<$dow1;$d++) echo "<td class=\"hd\"> </td>";
$c = $dow1;
for ($d=1; $d<=$dim; $d++, $c++) {
if ($c%7==0) echo "</tr><tr>";
$cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';
$st = ($d == $today) ? "style='padding: 0px;'" : '';
echo "<td class=\"$cl\" $st>\n";
echo "$d" ;
echo "</td>\n";
}
while ($c++ % 7 != 0) echo '<td class=\"hd\"> </td>';
echo "</tr></table>\n";
echo '</div></div>';
You can do this in your for...while loop:
if (date('d/m/y') == '16/08/14') {
echo 'Your table cell here';
}
you can find more information about the date function here, http://php.net/manual/en/function.date.php

Order By Year with Date field Mysql/PHP

I am having trouble with getting my year to echo properly.
Example: I have 2 news articles one with a year of 2011 and the other 2012. They are splitting into the correctly "year" div. But when it comes to rendering out the year, it works. But 2011 and 2012 are stacked above each other. Something like this.
2012
2011
5.1.12 My news article
Where 2011 year is suppose to go
5.1.11 My news article
Here is my code.
<?
$startYear = 2010;
$endYear = 2012;
for($y = $endYear; $y > $startYear -1; $y--) { ?>
<?
$news = query("SELECT * FROM news_entries
WHERE title > ''
AND published = 1
AND date >= '" . mktime(0,0,0,0,0,$y) . "'
AND date < '" . mktime(0,0,0,0,0,$y+1) . "'
ORDER BY date DESC");
?>
The rest of the query.
<? if($news['total']>0) { ?>
<h3><? echo $y; ?></h3>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<th class="first" width="125">Date</th>
<th>News Headline</th>
<th width="140"> </th>
</tr>
<? do { ?>
<tr>
<td><span><? echo date("F d, Y",$news['data']['date']); ?></span></td>
<td><? echo $news['data']['headline']; ?></td>
<td>Learn More</td>
</tr>
<? } while($news['data'] = mysql_fetch_assoc($news['object'])); ?>
<? }} ?>
<tr>
<td colspan="2" class="last"></td>
<td class="last"> Back to Top</td>
</tr>
</table>
I agree with Kato's comment. Typically, you get all of the results and iterate over them, printing a new header when the data changes.
Here's how this type of thing is usually handled (pseudocode):
$sql = "SELECT * FROM news_enteries ORDER BY date DESC";
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind variables
...
// Execute query
$stmt->execute() or die();
$year_header = "";
while ($row = $stmt->fetch()) {
$year = date("Y", strtotime($row['date']));
// Do we need a new year header?
if ($year_header <> $year) {
$year_header = $year;
// Print year header
echo $year_header;
}
// Process rest of the row
...
}

Categories