Home > Mobile >  How to return TRUE with time()?
How to return TRUE with time()?

Time:01-18

How to return TRUE when an account was created more than 30 days ago?

I have the following date:

$udata['joined']; - record date in time():

I tried like this

If($udata['joined'] = strtotime (" 30 days", time())){
    return true;
}

Any idea why it's not working correctly?

Return empty.

CodePudding user response:

I guess you want

If timestamp is smaller than (or exactly) 30 days ago

if ($udata['joined'] <= strtotime("-30 days", time()) {
    return TRUE;
}

(you need to substract 30 days from now and remove all syntax errors)

CodePudding user response:

Your current code is comparing the value of $udata['joined'] to the value of strtotime(" 30 days", time()), which is the timestamp for 30 days in the future from the current time. This comparison will always return false, because it is highly unlikely that the date of account creation is in the future.

To check if an account was created more than 30 days ago, you should use the strtotime("-30 days", time()) instead. This will give you the timestamp for 30 days ago from the current time. Then you can compare $udata['joined'] with that timestamp.

$thirtyDaysAgo = strtotime("-30 days", time());
if ($udata['joined'] < $thirtyDaysAgo) {
    return true;
}

This will check if the 'joined' date is less than 30 days ago, so it will return true if the account was created more than 30 days ago.

CodePudding user response:

You are assigning a value instead of using an operator, i.e. you are using = instead of ==. Try this:

If($udata['joined'] == strtotime (" 30 days", time())){
    return true;
}

Edit

As others pointed out, checking for equality will most likely always return false anyway, because you'd be very luck to hit the exact same timestamp!

What you are looking for is the <= (less than or equal) operator to check if $udata['joined'] is a timestamp before 30 days ago. In other words :

// true if the provided date is before 30 days ago
return strtotime($udata['joined']) < strtotime("-30 days", time());

CodePudding user response:

The issue with your code is that you are using the assignment operator (=) instead of the comparison operator (==) in your if statement. The assignment operator assigns a value to a variable, whereas the comparison operator compares two values to see if they are equal.

Also the comparison you are trying to make is incorrect. You are trying to check if the date the account was created is exactly 30 days in the future from the current time. Instead you should check if the date the account was created is more than 30 days in the past from the current time.

You can use something like this:

if(time() - strtotime($udata['joined']) > 30*24*60*60) {
    return true;
}
  •  Tags:  
  • php
  • Related