Subscribtion Query - where's it gone wrong?

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:

Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

Here's all the code.
The premise is that a person registers and gets 14 days for free.
After those 14 days, they must subscribe - they then get the system for 31 days.... until which time they are told to subscribe again.

Today's date as this is written on the forum is 1st October 2009.
The date of my subscription is 2050-09-24, I should therefore never be asked to subscribe - but yet today, being the 1st, I am.

Much of this code was supplied with the help of people on this forum - converting dates to time etc, to do calculations. Can someone tell me where this has gone wrong in the calculation as I don't see any problem.

Code: Select all

<?php
include "dbconn.php";
$email = $_POST['email'];
$password = $_POST['password'];
$email=mysql_real_escape_string($_POST["email"]);
$password=mysql_real_escape_string($_POST["password"]);
$result = mysql_query ("SELECT * FROM dxusers WHERE email = '$email' AND password = '$password'");
 
if (mysql_num_rows($result)==0)
 
{ echo "<meta http-equiv='Refresh' content='0 ;URL=index.html'>";}
 
elseif (mysql_num_rows($result)!=0)
{
while ($row = mysql_fetch_object($result)) 
    {
  $today = (date('Y-m-d'));
  $regdate = strtotime("$row->datejoined");
  $regdatecomplete = date( "Y-m-d", ($regdate) );
 
  $subRegDateMicrotime = strtotime($regdatecomplete);
  $fortnight = date( "Y-m-d", ($subDateRegMicrotime + (86400*14)) );
  
  
  $subDateMicrotime = strtotime($row->subscribed);
  $endmonth = date( "Y-m-d", ($subDateMicrotime + (86400*31)) );
  
  $sysyear = substr("$today",-10,4);
  $sysmonth = substr("$today",-5,2);
  $sysday = substr("$today",-2,2);
  
  if
  (
  ($sysmonth == "09") || 
  ($today <= $fortnight) || 
  ($today <= $endmonth)
  )
  
    {
$user = "$row->firstname";
$id = "$row->id";
setcookie("user", $user, time()+13600);
setcookie("firstname", $row->firstname, time()+13600);
setcookie("lastname", $row->lastname, time()+13600);
setcookie("email", $email, time()+13600);
setcookie("userid", $row->id, time()+13600);
setcookie("type", $row->type, time()+13600);
 
mysql_query("UPDATE dxusers SET status = 'online' where email = '$email' AND password = '$password'");
 
if ($row->type == "admin") 
      {
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=home&menu=home&title=welcome to the dealer exchange'>";
      }
 
elseif ($row->type == "user") 
      {
  
      $resultstartup = mysql_query ("SELECT * FROM dxvehicles WHERE userid = '$id'");
      if (mysql_num_rows($resultstartup)==0)
      { echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=vehicleadd&menu=myvadd&title=sell first vehicle'>";}
      elseif (mysql_num_rows($resultstartup)!=0)
      { echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=home&menu=home&title=welcome to the dealer exchange'>";}
      mysql_free_result($resultstartup);
      
      }
if ($row->type == "advertiser") 
      {
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=advertlist&menu=advert&title=welcome to the dealer exchange: advertising'>";
      }
      
    }
else
      { echo "<meta http-equiv='Refresh' content='0 ;URL=subscribe.php?id=$row->id'>"; }
  }
}
 
    mysql_free_result($result);
    mysql_close($sqlconn);
    
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Code: Select all

<?php
  $today = (date('Y-m-d'));
  $regdate = strtotime("2009-08-12");
  $regdatecomplete = date( "Y-m-d", ($regdate) );
 
  $subRegDateMicrotime = strtotime($regdatecomplete);
  $fortnight = date( "Y-m-d", ($subDateRegMicrotime + (86400*14)) );
  
  
  $subDateMicrotime = strtotime(2050-09-24);
  $endmonth = date( "Y-m-d", ($subDateMicrotime + (86400*31)) );
  
  echo "$endmonth";
  ?>
This echos a result of
2009-11-01
1) why is it not 31 days on.
2) why isn't it 2050?

I'm all confused.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by pickle »

Wow - that looks pretty convoluted. Line 10: strtotime() is getting passed the argument "2017", which is 2050 minus 9 minus 24. Try putting that in quotes.

Why are you storing the time as yyyy-mm-dd? Why not as a MySQL datetime stamp or a UNIX timestamp? It would make things simpler. If you can't change that, do this:

Code: Select all

$regdate = strtotime("2009-08-12");
$fortnight = strtotime('+2 weeks',$reg_date);
$endmonth = strtotime('+31 days',$reg_date);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

I tried all sorts of timestamps, but so much is now written in, it would be a right pain to change it.

I have used your method and tried this

Code: Select all

 $today = (date("Y-m-d"));
  
  $regdate = strtotime($row->datejoined);
  $subsdate = strtotime($row->subscribed);
  $fortnight = strtotime('+2 weeks',$regdate);
  $endmonth = strtotime('+31 days',$subsdate);
  
  $sysyear = substr("$today",-10,4);
  $sysmonth = substr("$today",-5,2);
  $sysday = substr("$today",-2,2);
  
  if
  (
  ($sysmonth == "09") || 
  ($today <= $fortnight) || 
  ($today <= $endmonth)
  )
Because I need to checked subscribed date after datejoined, but same problem.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

Ok - it partly worked. It's not liking the year 2050.

Code: Select all

<?php
  $today = (date('Y-m-d'));
  $regdate = strtotime("2009-08-12");
 
  $subsdate = strtotime("2050-05-22");
  $fortnight = strtotime('+2 weeks',$regdate);
  
  $thedate = strtotime($today);
  $endmonth = strtotime('+31 days',$subsdate);
  
  if ($thedate <= $endmonth) { echo "success";}
  elseif ($thedate >= $endmonth) { echo "subscribe";}
 
  echo "
  End date: $endmonth<br/>
  Today: $thedate";
  ?>
This produces:
subscribe End date: 2678400
Today: 1254351600
Why would that date be such a low number?

I then tried this:

Code: Select all

 $subsdate = strtotime("2028-12-31");
And that (after some testing to and fro), produces the highest number I can get to.

What is it about the 2050-??-?? number that it produces a low figure?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

This is a similar query - as it related to calculating times, so I have added it on, since you seem to know how to do this kind of things.

Code: Select all

$datesubmitted = strtotime($row->received);
$today = (date("Y-m-d"));
$thedate = strtotime($today);
$resulta = ($thedate - $datesubmitted);
$resultfinal = (date("$resultfinal"));
echo "$resultfinal";
This is meant to look at the date someone submitted something, and then I need to see how many days it has been since they submitted it (without needing to manually calculate.

14 days is the limit, so it would be good to make something go red if it is "15 days", but for now just need to see it show 12,13 days etc.

This code produces absolutely nothing at all.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by pickle »

The standard UNIX timestamp only goes to some time in 2038, so dates after that wrap around (for now).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Subscribtion Query - where's it gone wrong?

Post by simonmlewis »

Ahhhhhh that explains a whole lot.

Thank you very much.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply