/    Sign up×
Community /Pin to ProfileBookmark

creating a calendar using shell command ‘cal’

Ive been able to successfuly create a calendar using `cal` at the unix command line, with one small caveat however.

The first row of days of the calendar begins on the left hand side of the table. Without the necessary offset, that first row of days will never match up correctly with the row of days above it (i.e. Su Mo Tu We Th Fr Sa).

This is where my calendar is now: [url]http://mishii.no-ip.com/calendar.php[/url]

And this is my source:

[code=php]function make_calendar(){

exec(“cal”, $calArray);
$count = count($calArray);

print(“$calArray[0]n”);

print(“<table dir=”LTR” bgcolor=”#000000″ border=”1″ cellpadding=”3″ cellspacing=”1″>n”);

$dayrow = explode(” “, $calArray[1]);
$dayrowcount = count($dayrow);

for($k=1; $k < $count; $k++) {
$split = explode(” “, $calArray[$k]);
print(“<tr valign=”right” align=”right”>n”);

foreach($split as $jnd) {
$count = count($split);

if($jnd) {
print(“<td bgcolor=”#FFFFFF” align=”center”>$jnd</td>n”);
}
}

print(“</tr>”);
}
}[/code]

Ive exahsted my resources. Ive tried to look for help on google but people want to give you solutions that have integrated flat file DB support or MySQL. My problem is alot more simple than that.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 16.2006 — I came up with this, treating each line as a fixed-length record (instead of space-delimited record):
[code=php]
function make_calendar()
{
exec("cal", $calendar);
if(count($calendar > 1))
{
echo "<table dir='LTR' border='1' cellpadding='3' cellspacing='1'>n";
echo "<caption>{$calendar[0]}</caption>n";
for($line=1; $line<=count($calendar); $line++)
{
if(trim($calendar[$line]) != "")
{
echo "<tr>";
for($ix=0; $ix<19; $ix+=3)
{
echo "<td>" . substr($calendar[$line], $ix, 2) . "</td>n";
}
echo "</tr>n";
}
}
echo "</table>n";
}
}
[/code]
Copy linkTweet thisAlerts:
@matt1776authorJun 16.2006 — this is pure gold. ?

i will need to internalize this.

*walks away scratching his head mumbling ". . . fixed length?"*

Thanks ? this actually relates to a great many parsing situations ive encounterd before . . .
Copy linkTweet thisAlerts:
@matt1776authorJun 16.2006 — actually, im not sure I understand whats going on here:

[code=php] for($ix=0; $ix<19; $ix+=3)
{
echo "<td>" . substr($calendar[$line], $ix, 2) . "</td>n";
} [/code]
Copy linkTweet thisAlerts:
@NogDogJun 16.2006 — actually, im not sure I understand whats going on here:

[code=php] for($ix=0; $ix<19; $ix+=3)
{
echo "<td>" . substr($calendar[$line], $ix, 2) . "</td>n";
} [/code]
[/QUOTE]

Each line of the calendar is laid out as:
<i>
</i> 1111111111
01234567890123456789 &lt;-- string position
5 6 7 8 9 10 11 &lt;-- data

The substr() is grabbing the 2 characters (the last arg) starting at position $ix in the current line (remembering that numbering starts a 0 for the first character). So the for loop is starting with the first 2 characters (the first being a blank in this example), then incrementing up 3 positions for each iteration through that line (there is a space between each set of two characters). Fortunately if we specify a starting number greater than the last character in the string, substr() returns nothing, so we don't have to worry about the last line if there are fewer than 7 days.
Copy linkTweet thisAlerts:
@matt1776authorJun 16.2006 — How did you get this though? Thats what I was having issues with:

[CODE] 1111111111
01234567890123456789 <-- string position
5 6 7 8 9 10 11 <-- data[/CODE]
Copy linkTweet thisAlerts:
@NogDogJun 16.2006 — I ran the "cal" command and counted characters (real low tech stuff ? ). Here's a real simple script to do that:
[code=php]
<?php
header("Content-Type: text/plain"); // display plain text instead of HTML
echo "123456789012345678901234567890n"
echo cal; // back-tick operators same as shell_exec()
?>
[/code]
×

Success!

Help @matt1776 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...