Date() not showing the right time

I’m still confused as to why you want to do that. You just store when the subscription starts and expires then you can check in the code when a subscription has ended. No updates are required here at all.

In pseudo code:

if (now() > expiry_date) {
refuse_access()
}

1 Like

I was confused because i thought that you can’t use now () with php syntax as mentioned above because I tried to echo now (); but it doesn’t work

You can’t. As I stated I was using pseudo code, which is a mix of natural language and programming constructs like if used to get a point across easily. It doesn’t run in any programming language.

2 Likes

Carbon is a really great library for dealing with dates.

It is, but I would always encapsulate it in a custom class so you keep the possibility of swapping it out later open. Instead of sprinkling it all over te place making a refactor near impossible.

Why would you ever need to swap it out? It isn’t like a database where someone is going to demand a change. Its just a date library. I think that is kind of taking things to far.

In case the maintainer decides to stop maintaining it, or in case a different library comes along that is even better/faster/etc.

It is quite easy to create a wrapper for it, and there are many benefits to doing so. Like being able to create mocks for your own interface. Or if you found a bug you need to work around you can work around it once, not every time you call the bugged code.

1 Like

I have been trying to follow this tutorial in how to create a member’s expiry system and I can get it to work with my single subscription service but can’t do this for my multiples services… I have the following database table and a user can select level 1 in either subscription 1, 2 or 3. I guess I would need to check to make sure that each one contains this service and then to use the appropriate date for that service…So, subscription should match subscriptionplandate, subscription2 should match subscriptionplandate2 etc


<!DOCTYPE html>
<html>
<head>
   <title></title>
   <meta charset="utf-8">
   <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>










<?php

session_start();

if(!isset($_SESSION['u_uid'])) {
  header("Location: index.php?notlevel1user");
  exit();
} else {
  include_once 'includes/dbh.php';






                            $sql = "SELECT * FROM user_lessonsubscription WHERE user_uid = '".$_SESSION['u_uid']."'";
                            $result = mysqli_query($conn, $sql);
                            $resultCheck = mysqli_num_rows($result);
                            if($resultCheck > 0) {
                               while($row = mysqli_fetch_assoc($result)) {
                                  
                                     if ($row['subscriptionplan'] == 'Level 1') {
                                     $userregdate = $row['subscriptionplandate'];
                                    $subscriptionends = date("Y-m-d", strtotime(date("Y-m-d", strtotime($userregdate)). " + 2 day"));
                                    if (date("Y-m-d") < $subscriptionends) {
                                      echo 'Your plan is still valid';
                                      exit();
                                     } else {
                                         if ($row['subscriptionplan2'] == 'Level 1') {
                                         $userregdate2 = $row['subscriptionplandate2'];
                                         $subscriptionends2 = date("Y-m-d", strtotime(date("Y-m-d", strtotime($userregdate2)). " + 2 day"));
                                          if (date("Y-m-d") < $subscriptionends2) {
                                          echo 'Your plan is still valid';
                                          exit();
                                      } else {
                                          if ($row['subscriptionplan3'] == 'Level 1') {
                                          $userregdate3 = $row['subscriptionplandate3'];
                                          $subscriptionends3 = date("Y-m-d", strtotime(date("Y-m-d", strtotime($userregdate3)). " + 2 day"));
                                           if (date("Y-m-d") < $subscriptionends3) {
                                           echo 'Your plan is still valid';
                                           exit();
                                     } else {
                                         echo 'Your plan has expired';
                                     }
                                     } 
                                     }
                                   }
                                    }
                                  }
                                }
                              }
                            }
                             

       
                                     

The video is as follows:

This is my database table:

Again, as has been advised several times, the database layout you have chosen is making things more difficult than they need to be. You need to separate the plan details from the user details.

Allowing the “Level 1” to be in any plan, along with “primer”, “Level 2” and “Level 3” gives you a large number of combinations to check. If you separate these out to another table, I think it would make it easier to check things.

But, I can’t see what’s wrong with the code you posted - could you describe what it isn’t doing for you? Weren’t you storing expiry dates at some point? So why the need to calculate the expiry date based on the user registration date?

You could simplify your checks a little by doing something kludgy like this, if all you want to know is whether one of your three columns has a non-expired “Level 1” subscription:

$suffix = array("", "2", "3"); 
$active = false;
foreach ($suffix as $ending) { 
  if ($row['subscriptionplan' . $ending] == "Level 1") { 
    $userregdate = $row['subscriptionplandate' . $ending];
    $subend = date("Y-m-d", strtotime(date("Y-m-d", strtotime($userregdate)) . " + 2 days"));
    if (date("Y-m-d") < $subend) {
      $active = true;
      }
    }
}
if ($active) {
...
} else {
...
}

So loop through the three pairs of start date and plan levels instead of laboriously typing everything out long-hand.

It would be “nicer” if you consistently named the columns to end in “1”, “2” or “3”. Or put them in another table where you could probably do the whole thing in a query and just get a “true” or “false” back to check if there’s a valid subscription.

1 Like

I think I have an idea but you are correct and I should rename my database columns to make it easier to understand… sorry for the confusion…

It’s not about renaming columns. It’s about creating one table for users and another for subscription and not stuffing everything into one giant table.

2 Likes

Part of the root of the problem. It has also been mentioned previously to avoid at all cost out-dated tutorials that teach things the wrong way.
You can see from the beginning of that video they are using the old mysql functions there, exactly the kind of tutorial I advised to avoid. I could not help but take a peek at their log-in system tutorial too. That also uses mysql and stores un-hashed passwords :scream:
These are superb examples of how not to do things.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.