In this tutorial, we will create a Get Days Left Before Christmas using PHP. This code will automatically calculate the remaining days before the incoming Christmas holiday. The system uses a PHP POST method to initiate a specific formula that displays the remaining days left before Christmas by calculating between the given current date subtracted by the target date divided by 86400. This is a user-friendly kind of program feel free to modify it.
We will be using PHP as a scripting language and interpreter that is used primarily on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can easily be used by the next generation.
Getting Started:
First, you have to download & install XAMPP or any local server that run PHP scripts. Here’s the link for XAMPP server https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Creating The Interface
This is where we will create a simple form for our application. To create the copy of the form and write it into your text editor, then save it as index.php.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Get Days Left Before Christmas</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-3"></div> <div class="col-md-6"> <form method="POST" action=""> <div class="form-group"> <label>Enter date</label> <input type="date" name="date" class="form-control" required="required"/> </div> <center><button class="btn btn-primary" name="get">Generate</button></center> </form> <?php include'get_days.php'?> </div> </div> </body> </html>
Creating the Main Function
This code contains the main function of the application. This code will display the remaining days before christmas when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as get_days.php
<?php date_default_timezone_set('Etc/GMT-8'); if(ISSET($_POST['get'])){ $target=date('Y-12-25'); $date= date('Y-m-d', strtotime($_POST['date'])); if ($date > $target) { $target = date('Y-12-25', strtotime($target . ' +1 year')); $date1 = $target; $date2 = $date; } else { $date1 = $date; $date2 = $target; } $days=(int)(strtotime($target) - strtotime($date)) / 86400; echo "<center><h2><span class='text-primary'>".$days."</span> days left before christmas day</h2></center>"; } ?>
There you have it we successfully created Get Days Left Before Christmas using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!