How do you calculate if one date is 14 days within another?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you calculate if one date is 14 days within another?

Post by simonmlewis »

Code: Select all

    $timeStamp = strtotime("$row->subscribed");
  $timeStamp += 24 * 60 * 60 * 14;
  $subscribecalc += 24 * 60 * 60;
  $fortnight = date("Y-m-d", $timeStamp);
  $subscribed = date("Y-m-d", $subscribecalc);
if ($subscribed <= $fortnight) { ..........
 
I need to see if someone's subscribed date is 14 days old.
For example:
Fred subscribed on 2009-05-15, and today is 2009-05-30.
In this scenario they should be rejected.
If today's date is 2009-05-29, then they are accepted.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: How do you calculate if one date is 14 days within another?

Post by spider.nick »

simonmlewis wrote:

Code: Select all

$timeStamp = strtotime("$row->subscribed");
  $timeStamp += 24 * 60 * 60 * 14;
  $subscribecalc += 24 * 60 * 60;
  $fortnight = date("Y-m-d", $timeStamp);
  $subscribed = date("Y-m-d", $subscribecalc);
  if ($subscribed <= $fortnight) { ..........
 
I need to see if someone's subscribed date is 14 days old.
For example:
Fred subscribed on 2009-05-15, and today is 2009-05-30.
In this scenario they should be rejected.
If today's date is 2009-05-29, then they are accepted.
Really, these are logic questions that you could probably answer yourself. But, I will give you the benefit of the doubt.

The easiest way, since you can compare two integers, would be:

Code: Select all

if( $timeStamp < time() )
Let me know if that does the trick. If it does, you know the routine for marking this thread as resolved.

Nick
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you calculate if one date is 14 days within another?

Post by simonmlewis »

Sorry, I don't understand. But this seems to work.
Just change the $regdate to pretend the person registered earlier, and it becomes "no". Set the date to a date within the 14 days, and it says "yes".

Code: Select all

$today = (date('Y-m-d'));
 
$regdate = strtotime('2009-07-07');
$regdatecomplete = date( "Y-m-d", ($regdate) );
 
$subDateMicrotime = strtotime($regdatecomplete);
$fortnight = date( "Y-m-d", ($subDateMicrotime + (86400*14)) );
 
echo "Today: $today";
echo "<br/>Registered date: $regdatecomplete";
echo "<br/>In 14 days: $fortnight"; 
 
if ($today <= $fortnight) { echo "<br/>yes";} else { echo "<br/>no";}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you calculate if one date is 14 days within another?

Post by requinix »

I'm going to step in here and rant.

Do not go around adding X seconds to a date in order to get something Y days into the future. You are doing it wrong.
Example:

Code: Select all

$date = strtotime("2009-10-25"); // Sunday
$plustwoweeks = $date + 86400*14;
echo date("D Y-m-d", $plustwoweeks); // Saturday 2009-11-07
The correct way to do this is with strtotime, or mktime if you feel like separating the date into year/month/day parts.

Code: Select all

$date = strtotime("2009-10-25"); // Sunday
$plustwoweeks = strtotime("+2 weeks", $date);
echo date("D Y-m-d", $plustwoweeks); // Sunday 2009-11-08
 
$year = 2009; $month = 10; $day = 25;
$plustwoweeks = mktime(0, 0, 0, $month, $day + 14, $year);
echo date("D Y-m-d", $plustwoweeks); // Sunday 2009-11-08
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you calculate if one date is 14 days within another?

Post by simonmlewis »

Here's the code so far for the whole thing:

Code: Select all

$today = (date('Y-m-d'));
  $regdate = strtotime($row->subscribed);
  $regdatecomplete = date( "Y-m-d", ($regdate) );
 
  $subDateMicrotime = strtotime($regdatecomplete);
  $fortnight = date( "Y-m-d", ($subDateMicrotime + (86400*14)) );
    
  $year = substr("$row->subscribed",-10,4);
  $month = substr("$row->subscribed",-5,2);
  $day = substr("$row->subscribed",-2,2);
  
  $today = (date('Y-m-d'));
  $sysyear = substr("$today",-10,4);
  $sysmonth = substr("$today",-5,2);
  $sysday = substr("$today",-2,2);
    
  $endyear = $year;
  $endmonth = ($month + 1);
  $endday = $day;
  
  if 
  (($month == "08" && $sysmonth == "08") || 
  ($today <= $fortnight) || 
  ($sysyear >= $endyear && $sysmonth <= $endmonth && $sysday >= $endday))
Here's the code.
It asks if they registered in August and if it is still Aug. That's simple.
At the end, it asks about the system day, that's fine.

It's the bit about the fortnight which is the pain here. I think I am grabbing the subcription date fromthe db wrong somehow.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply