bigdaveygeorge
bigdaveygeorge

Reputation: 999

Conditional if registration date in strtotime is more than x months logic

I want to show a message if a user has been registered for longer than x months.

I intended to do this by getting the registered date and current date using strtotime and then seeing if the registered date is less than the current date minus the months.

Outcome of the below is 'Registered for more than 3 months' but the registered date is less than 3 months.

Feel free to rearrange the logic if it helps to find an answer.

<?php

$registered_date = strtotime( date( 'ymd', strtotime('2017-04-01 10:39:45') ) );
$current_date = strtotime( date( 'ymd' ) );
$months = '3';
$current_date_minus_months = strtotime( '-' . $months . ' months' , $current_date);

echo '<p>Registered Date: ' . $registered_date . '</p>';
echo '<p>Current Date: ' . $current_date . '</p>';
echo '<p>Current Date Minus Months: ' . $current_date_minus_months . '</p>';

if ( $current_date_minus_months < $registered_date ) {
    echo '<p>Registered for more than 3 months</p>';
} else {
    echo '<p>Registered for less than 3 months</p>';
}

?>

Upvotes: 1

Views: 463

Answers (2)

sidyll
sidyll

Reputation: 59297

You first line of core already gives the first problem:

$registered_date = strtotime( date( 'ymd', strtotime('2017-04-01 10:39:45') ) );

The following code:

date('ymd', strtotime('2017-04-01 10:39:45'))

Results in 170401. Then you feed this to strtotime() again. This probably gets parsed as 17:04:01, meaning 5pm of today in your local timezone. The truth is: strtotime already converts to Unix timestamp, so there is no point in converting a date string (2017-04-01 10:39:45) to this format, then convert it back with date and then back again to Unix with another strtotime call.

Then, you also have a reversed logic: if the current date minus 3 months event happens equal of after the registered date, that means the user registration is 3 months or longer. But this line states the opposite:

if ( $current_date_minus_months < $registered_date ) {

Either use >= or switch the variables around.

Also, the only point in saving the current date is that you use it in your calculation for 3 months ago. But using the current time as second parameter is the default, so you don't need that.

Cleaning up your code, we have this:

<pre><?php

$months = 3;

$registered_date = strtotime('2017-04-01 10:39:45');
$current_date_minus_months = strtotime("-$months months");

# for your debugging
print_r([
    'registered_date'      => $registered_date,
    'current_date'         => time(),
    'current_minus_months' => $current_date_minus_months,
]);

if ( $registered_date < $current_date_minus_months )
    echo "Registered for more than 3 months\n";
else
    echo "Registered for less than 3 months\n";

Anyway, a much better approach to this is to use the DateTime interface, object oriented, as described in the other answer. Here is another way of doing it:

$months = 3;

$current    = new DateTime();
$registered = new DateTime('2017-01-01 10:39:45');
$threshold  = $current->sub(new DateInterval('P' . $months . 'M'));

if ($registered <= $threshold)
    echo "more or equal to $months months";
else
    echo "less than $months months";

Upvotes: 1

wormi4ok
wormi4ok

Reputation: 632

It's better to use Php Datetime class to work with dates. In your case it will look like this:

<?php

$registered_date = new Datetime('2017-04-01 10:39:45');
$current_date = new Datetime();
$months = '3';
$diff = $current_date->diff($registered_date);

echo '<p>Registered Date: ' . $registered_date . '</p>';
echo '<p>Current Date: ' . $current_date . '</p>';
echo '<p>Month diff: ' . $diff->m . '</p>';

if (  $diff->y > 0 || $diff->m >= $months) {
    echo '<p>Registered for more than 3 months</p>';
} else {
    echo '<p>Registered for less than 3 months</p>';
}

?>

Upvotes: 1

Related Questions