Page 1 of 1
How do you calculate if one date is 14 days within another?
Posted: Wed Jul 22, 2009 3:15 pm
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.
Re: How do you calculate if one date is 14 days within another?
Posted: Wed Jul 22, 2009 3:31 pm
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:
Let me know if that does the trick. If it does, you know the routine for marking this thread as
resolved.
Nick
Re: How do you calculate if one date is 14 days within another?
Posted: Wed Jul 22, 2009 3:43 pm
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";}
Re: How do you calculate if one date is 14 days within another?
Posted: Wed Jul 22, 2009 3:54 pm
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
Re: How do you calculate if one date is 14 days within another?
Posted: Wed Jul 22, 2009 4:09 pm
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.