Software, your way.
burger menu icon
WillMaster

WillMasterBlog > PHP

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 Weeks Between Dates?

The idea for this article and its accompanying software was triggered by the thought of an important Possibilities ezine anniversary that is coming up.

Because Possibilities is published every week, the software calculates the number of weeks between two dates.

My thought is that knowing the number of weeks may be helpful for other things, too. Anything that is measured in weeks might be a candidate.

A year has more days than just 52 weeks worth. Did you know that? It affects calculations when the period is a number of years.

Every year, there are 52 weeks plus 1 day. On leap years, there are 52 weeks plus 2 days.

So, every 5 or 6 years you get an extra week.

A working installation is available. See the How Many Weeks Between Dates? demo.

The source code is below. Copy it and save it with a .php file name. Upload it to your server and types its URL into your browser's address bar.

<?php
/*
How Many Weeks Between Dates?
Version 1.0
July 8, 2024
Will Bontrager Software LLC
https://www.willmaster.com/
*/
?><!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How Many Weeks?</title>
<style type="text/css">
body { font-size:100%; font-family:sans-serif; background-color:white; }
a { text-decoration:none; }
</style>
</head>
<body>

<h1 style="text-align:center;">
<a href="https://www.willmaster.com/">
<img src="https://www.willmaster.com/images/wmlogo_icon.gif" alt="Willmaster logo">
</a>
How Many<br>Weeks Between Dates?
</h1>

<form id="my-form" action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post">
<div style="display:table; margin:0 auto;">
<div style=text-align:right;">
From date: <input type="date" name="fromdate"><br>
To date: <input type="date" name="todate">
</div>
<p style="text-align:center;">
<input type="submit" name="submitter" value="Calculate It">
</p>
</div>
</form>

<div style="display:table; margin:0 auto; border:1px solid #999; border-radius:.5em; padding:.75em;">
<?php
if( isset($_POST['submitter']) )
{
   $today = date('Y-m-d');
   if( ! preg_match('/\d/',$_POST['fromdate']) ) { $_POST['fromdate'] = $today; }
   if( ! preg_match('/\d/',$_POST['todate']) ) { $_POST['todate'] = $today; }
   $date1=mktime(0,0,0,intval(substr($_POST['fromdate'],5,2)),intval(substr($_POST['fromdate'],-2)),intval(substr($_POST['fromdate'],0,4)));
   $date2=mktime(0,0,0,intval(substr($_POST['todate'],5,2)),intval(substr($_POST['todate'],-2)),intval(substr($_POST['todate'],0,4)));
   $diff = abs($date2-$date1) / (60*60*24);
   $wks = $diff/7;
   $int = intval($wks);
   echo "Results:<br>Number of days is $diff<br>Number of weeks is $wks<br>Number of whole weeks is $int";
}
else
{
   echo '[Results of calculation will be here.]';
}
?>
</div>
<p style="text-align:center;">
&copy; 2024 <a href="https://www.willmaster.com/">Will Bontrager Software LLC</a>
</body>
</html>

To test the demo or your installation of the software, the Possibilities ezine's publication length can be calculated.

Enter July 27, 1999 as the first date. And July 27, 2024 as the second date. The result will be the number of weeks in 25 years. The number of weeks is also the ezine's 25th anniversary issue number.

If you get the answer "Number of whole weeks is 1304", your installation of the software does the calculations correctly.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post 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 Blog 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.

Recent Articles in the Library

Extracting Dates From the Unix Timestamp

The UNIX timestamp can be used to determine the represented time for any time zone on Earth.

Submit Form Data Without Bothering User

To silently and effectively submit a form on a web page, Ajax with FormData() can be used.

CSS Dots

CSS dots can be used for interactions between web page and website user.

Email Testing

One thing to test when email doesn't get sent or doesn't arrive is whether the server actuall sends email.

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

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