Brent
Brent

Reputation: 2485

Date Check Amount of Time Passed

I am trying to check if the user has been on the site longer then 6 months, if they have to prompt a message.

I cannot get this to work, it keeps thinking 6 weeks has passed even when the date is moved to today.

Currently my code.

            $created_at = "2014-12-01 16:58:23";

            $sixweek = 604800 * 6;

            if(strtotime($created_at) < time() + ($sixweek))
            {
                $data['needsnewimage'] = 1;
                die('6 Weeks has passed ');

            }else{
                $data['needsnewimage'] = 0;
                die('6 Weeks has not passed');
            }

Any ideas?

Upvotes: 0

Views: 52

Answers (2)

vascowhite
vascowhite

Reputation: 18440

DateTime will make this simple, something like:-

$created_at = new \DateTime("2014-12-01 16:58:23");
$sixWeeks = new \DateInterval('P6W');
if($created_at->add($sixWeeks) < new \DateTime()){
    echo "Six weeks has passed!\n";
} else echo "Not yet!\n";

See it working
Reference

Upvotes: 1

Marc B
Marc B

Reputation: 360572

That's not going to work. Consider this: someone signs up for your site TODAY:

$created_at = '2014-12-01 08:00:00'; // "today"

if ($created_at < $today + $sixweek) ...

becomes

if ($today < $today + $sixweek)
if (0 < $sixweek)

and will always be true.

You want

if ($created_at > (time() - $sixweek))
                ^---------^

Note the changed math

Upvotes: 2

Related Questions