Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - Display a unix calendar 1

Status
Not open for further replies.

thewhistler1

Technical User
Jan 20, 2008
35
0
0
US
I am trying to display a calendar on my web page, just the current month. I think the easiest way to do this would be to just display a unix calendar with the cal command. I did this

$cal = `cal`;
$cal = nl2br($cal);
echo $cal;

but the numbers don't line up correctly.

Any one know how to make this look right or another simple way to display this months calendar?

Thanks for the help.
 
Hi

Yes, because whitespace characters are collapsed in HTML rendering.

Either display it with preserved whitespaces ( surrounded with [tt]pre[/tt] tags or with [tt]white-space: pre[/tt] style ), or split it up and put each day in its own [tt]table[/tt] cell.

Feherke.
 
OK

How would I go about preserving the white space?

Thanks
 
Hi

thewhistler1 said:
How would I go about preserving the white space?
Feherke said:
surrounded with [tt]pre[/tt] tags
Code:
$cal =  `cal`;
$cal = nl2br($cal);
echo [red]"<pre>[/red]$cal[red]</pre>"[/red];
Feherke said:
or with [tt]white-space: pre[/tt] style
Code:
$cal =  `cal`;
$cal = nl2br($cal);
echo [red]"<p style=\"font-family: monospace; white-space: pre\">[/red]$cal[red]</p>"[/red];


Feherke.
 
Oh OK, I had never used the <pre> tags so I didn't follow what you meant. That worked.... thanks a lot.
 
Hi

Feherke said:
split it up and put each day in its own [tt]table[/tt] cell.
Anyway, let us have this solution too.
Code:
$cal=`cal`;

foreach (split("\n",$cal) as $nr=>$line) if ($nr>1 && $line) {
  preg_match_all("/(..)./",$line,$match);
  echo "<tr><td>",join("</td><td>",array_map(create_function('$o','return trim($o)?$o:"&nbsp;";'),$match[1])),"</td></tr>\n";
}

Feherke.
 
Hi

Oops, submitted too soon.
Code:
foreach (split("\n",$cal) as $nr=>$line) if ($nr>1 && $line) {
  preg_match_all("/(..).[red]?[/red]/",$line,$match);
  echo "<tr><td>",join("</td><td>",array_map(create_function('$o','return trim($o)?$o:"&nbsp;";'),$match[1])),"</td></tr>\n";
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top