Simple php calendar with any day offset

I am coding a quite huge calendar script for various works I have in progress.
The barebones of this calendar I have coded today represent a php function that returns a calendar into an html table of any month of any year (according to php limits) with any day offset.
This means your calendar could start on sunday, monday, tuesday and so on.
It’s just the beginning but you can customize it with styles and other funny things.

Let’s see the code.

function calendar($year, $month, $day_offset = 0){ 
    $days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
    $months = array("january","february","march","april","may","june","july","august","september","october","november","december");
    $day_offset = $day_offset % 7;
    $start_day = gmmktime(0,0,0,$month,1,$year); 
    $start_day_number = date("w",$start_day);
    $days_in_month = date("t",$start_day);
    $final_html .= "\n\n";
    for($x=0;$x<=6;$x++){
    	$final_html .= "";
    }
    $final_html .= "\n";
    $blank_days = $start_day_number - $day_offset;
    if($blank_days<0){$blank_days = 7-abs($blank_days);}
    for($x=0;$x<$blank_days;$x++){
    	$final_html .= "";
    }
    for($x=1;$x<=$days_in_month;$x++){
    	if(($x+$blank_days-1)%7==0){
    		$final_html .= "\n";
    	}
    	$final_html .= "";
    }
    while((($days_in_month+$blank_days)%7)!=0){
    	$final_html .= "";
    	$days_in_month++;
    }
    $final_html .= "\n
".$months[$month-1]." $year
".$days[($x+$day_offset)%7]."
x
$xx
"; return($final_html); }

Since it’s just the beginning the code is quite simple, but if you have any question or suggestion, just leave a comment.

How will you use this function?

If you want the March 2006 calendar starting on Sunday, just

echo calendar(2006,3);

and the result is

\n

\n”;
for($x=0;$x<=6;$x++){ $final_html .= "

“;
}
$final_html .= “

\n”;
$blank_days = $start_day_number – $day_offset;
if($blank_days<0){$blank_days = 7-abs($blank_days);} for($x=0;$x<$blank_days;$x++){ $final_html .= "

“;
}
for($x=1;$x<=$days_in_month;$x++){ if(($x+$blank_days-1)%7==0){ $final_html .= "

\n

“;
}
$final_html .= “

“;
}
while((($days_in_month+$blank_days)%7)!=0){
$final_html .= “

“;
$days_in_month++;
}
$final_html .= “

\n

“.$months[$month-1].” $year
“.$days[($x+$day_offset)%7].”
x
$x x

“;
return($final_html);
}

echo calendar(2006,3);

?>

While if you want April 2006 starting from monday, just

echo calendar(2006,4,1);

and the result is

GRAHAM Sent me a code he modified to include cycling through months and years and converted it to div’s making it xhtml compliant.

Here it is





Calender



  • Back".$months[$month-1]." $yearNext
  • \n"; $final_html .= "
  • "; for($x=0;$x<=6;$x++){ $final_html .= "".$days[($x+$day_offset)%7].""; } $final_html .= "
  • \n"; $final_html .= "
    • \n"; $blank_days = $start_day_number - $day_offset; if($blank_days<0){$blank_days = 7-abs($blank_days);} for($x=0;$x<$blank_days;$x++){ $final_html .= " "; } for($x=1;$x<=$days_in_month;$x++){ if(($x+$blank_days-1)%7==0){ $final_html .= "
    • \n
    • "; } $final_html .= "$x"; } while((($days_in_month+$blank_days)%7)!=0){ $final_html .= " "; $days_in_month++; } $final_html .= "
    • \n
    "; return($final_html); } if(isset($_GET['y'])){ $y = $_GET['y']; } else { $y = date("Y"); } if(isset($_GET['m'])){ $m = $_GET['m']; } else { $m = date("m"); } echo calendar($y,$m); ?>

    Get the most popular Phaser 3 book

    Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

    Get the book

    214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
    // 1+2=3
    // 100 rounds
    // 10000000
    // 2 Cars
    // 2048
    // A Blocky Christmas
    // A Jumping Block
    // A Life of Logic
    // Angry Birds
    // Angry Birds Space
    // Artillery
    // Astro-PANIC!
    // Avoider
    // Back to Square One
    // Ball Game
    // Ball vs Ball
    // Ball: Revamped
    // Balloon Invasion
    // BallPusher
    // Ballz
    // Bar Balance
    // Bejeweled
    // Biggification
    // Block it
    // Blockage
    // Bloons
    // Boids
    // Bombuzal
    // Boom Dots
    // Bouncing Ball
    // Bouncing Ball 2
    // Bouncy Light
    // BoxHead
    // Breakout
    // Bricks
    // Bubble Chaos
    // Bubbles 2
    // Card Game
    // Castle Ramble
    // Chronotron
    // Circle Chain
    // Circle Path
    // Circle Race
    // Circular endless runner
    // Cirplosion
    // CLOCKS - The Game
    // Color Hit
    // Color Jump
    // ColorFill
    // Columns
    // Concentration
    // Crossy Road
    // Crush the Castle
    // Cube Jump
    // CubesOut
    // Dash N Blast
    // Dashy Panda
    // Deflection
    // Diamond Digger Saga
    // Don't touch the spikes
    // Dots
    // Down The Mountain
    // Drag and Match
    // Draw Game
    // Drop Wizard
    // DROP'd
    // Dudeski
    // Dungeon Raid
    // Educational Game
    // Elasticity
    // Endless Runner
    // Erase Box
    // Eskiv
    // Farm Heroes Saga
    // Filler
    // Flappy Bird
    // Fling
    // Flipping Legend
    // Floaty Light
    // Fuse Ballz
    // GearTaker
    // Gem Sweeper
    // Globe
    // Goat Rider
    // Gold Miner
    // Grindstone
    // GuessNext
    // Helicopter
    // Hero Emblems
    // Hero Slide
    // Hexagonal Tiles
    // HookPod
    // Hop Hop Hop Underwater
    // Horizontal Endless Runner
    // Hundreds
    // Hungry Hero
    // Hurry it's Christmas
    // InkTd
    // Iromeku
    // Jet Set Willy
    // Jigsaw Game
    // Knife Hit
    // Knightfall
    // Legends of Runeterra
    // Lep's World
    // Line Rider
    // Lumines
    // Magick
    // MagOrMin
    // Mass Attack
    // Math Game
    // Maze
    // Meeblings
    // Memdot
    // Metro Siberia Underground
    // Mike Dangers
    // Mikey Hooks
    // Nano War
    // Nodes
    // o:anquan
    // One Button Game
    // One Tap RPG
    // Ononmin
    // Pacco
    // Perfect Square!
    // Perfectionism
    // Phyballs
    // Pixel Purge
    // PixelField
    // Planet Revenge
    // Plants Vs Zombies
    // Platform
    // Platform game
    // Plus+Plus
    // Pocket Snap
    // Poker
    // Pool
    // Pop the Lock
    // Pop to Save
    // Poux
    // Pudi
    // Pumpkin Story
    // Puppet Bird
    // Pyramids of Ra
    // qomp
    // Quick Switch
    // Racing
    // Radical
    // Rebuild Chile
    // Renju
    // Rise Above
    // Risky Road
    // Roguelike
    // Roly Poly
    // Run Around
    // Rush Hour
    // SameGame
    // SamePhysics
    // Save the Totem
    // Security
    // Serious Scramblers
    // Shrink it
    // Sling
    // Slingy
    // Snowflakes
    // Sokoban
    // Space Checkers
    // Space is Key
    // Spellfall
    // Spinny Gun
    // Splitter
    // Spring Ninja
    // Sproing
    // Stabilize!
    // Stack
    // Stick Hero
    // String Avoider
    // Stringy
    // Sudoku
    // Super Mario Bros
    // Surfingers
    // Survival Horror
    // Talesworth Adventure
    // Tetris
    // The Impossible Line
    // The Moops - Combos of Joy
    // The Next Arrow
    // Threes
    // Tic Tac Toe
    // Timberman
    // Tiny Wings
    // Tipsy Tower
    // Toony
    // Totem Destroyer
    // Tower Defense
    // Trick Shot
    // Tunnelball
    // Turn
    // Turnellio
    // TwinSpin
    // vvvvvv
    // Warp Shift
    // Way of an Idea
    // Whack a Creep
    // Wheel of Fortune
    // Where's my Water
    // Wish Upon a Star
    // Word Game
    // Wordle
    // Worms
    // Yanga
    // Yeah Bunny
    // Zhed
    // zNumbers