Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

How Many Days Until ...

On your website, publish an always-correct number of days until a certain date.

  • A sale deadline.
  • A national holiday.
  • Your birthday.
  • Any date that has meaning to your site visitors.

The How Many Days Until ... PHP script included with this article does the calculation.

A regular URL with month and day parameters are used to call How Many Days Until … .

Example URL:

https://example.com/daysuntil.php?month=2&day=14

In this example, the script is called with the date of Valentines Day. The script will respond with the number of days until February 14. The response is always the number of days from the current day.

The month can be specified as a month number or with an English spelling. "2" and "February" are both the second month of the year.

Let's take January 1 as another example.

https://example.com/daysuntil.php?month=January&day=1

If the script is called on December 31, the number of days until New Years Day will be 1. On January 1, the number of days will be 0.

On January 2, the number of days until New Years Day will be 364 or, if a leap year, 365.

Live Example

The live example is for January 1.

There are 41 days until New Years Day.

The number of days in that example is updated every day.

Implementing It

The steps are (1) putting the How Many Days Until … script on your server and (2) using it on your web pages.

Putting How Many Days Until … on Your Server

Here is the script. Copy it. Name it numdays.php (or other PHP name you prefer). Upload it. And make a note of its URL.

<?php
/*
How Many Days Until ...
Version 1.0
November 9, 2020
Will Bontrager Software LLC
https://www.willmaster.com/
*/
// Expects GET request with parameters month=month (number or English spelling) and day=day (number)
if( empty($_GET['month']) or empty($_GET['day']) ) { echo 'Missing parameters.'; exit; }
$now = date('Y-m-d');
$nowYear = substr($now,0,4);
$month = trim($_GET['month']);
$day = preg_replace('/\D/','',$_GET['day']);
if( preg_match('/\D/',$month) )
{
   $month = substr(strtolower($month),0,3);
   switch($month)
   {
      case 'jan' : $month = 1; break;
      case 'feb' : $month = 2; break;
      case 'mar' : $month = 3; break;
      case 'apr' : $month = 4; break;
      case 'may' : $month = 5; break;
      case 'jun' : $month = 6; break;
      case 'jul' : $month = 7; break;
      case 'aug' : $month = 8; break;
      case 'sep' : $month = 9; break;
      case 'oct' : $month = 10; break;
      case 'nov' : $month = 11; break;
      case 'dec' : $month = 12; break;
      default    : $month = substr($now,5,2);
   }
}
if( $day < 1 or $day > 31 ) { $day = substr($now,8); }
if( $month < 1 or $month > 12 ) { $month = substr($now,5,2); }
if( intval($month)<10 ) { $month = '0' . intval($month); }
if( intval($day)<10 ) { $day = '0' . intval($day); }
$destdate = "$nowYear-$month-$day";
if( $destdate < $now )
{
   $nowYear++;
   $destdate = "$nowYear-$month-$day";
}
$origin = new DateTime($now);
$landing = new DateTime($destdate);
$daysdiff = $origin->diff($landing);
echo $daysdiff->format('%a');
exit;
?>

The URL of numdays.php that you just uploaded is required for publishing, on your web page, the number of days to the occurrence of a date.

Using it on Your Web Pages

Copy this code and make changes to it. Notes follow.

<?php 
echo(file_get_contents('https://example.com/numdays.php?month=January&day=1')) 
?>

To implement the above:

  1. Replace https://example.com/numdays.php with the URL of numdays.php on your server.

  2. Replace January with the month of the date you want to specify. The month may be the month number or the month with English spelling.

  3. Replace 1 with the day of the date you want to specify.

When those replacements have been made, paste the code into your web page.

The number of days from the current day to the month and day you specified will be published on the web page at the point where you pasted the above code. Every day, the number changes.

The Code of the Example

Here is the code (with placeholder URL) for the "January 1" example you see further above.

There are <?php 
echo(file_get_contents('https://example.com/numdays.php?month=January&day=1')) 
?> days until New Years Day.

The code for publishing the number of days can be used as many time as you wish on a web page. For demonstration, this table contains the number of days to two holidays.

DateHow
Many
Days
January 1 41
February 14 85

The month and day is always assumed to be during the 12 months beginning on the current day. That way, your code will be good year after year.

Every day, year after year, your code publishes the number of days to the next occurrence of a certain date.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC