user3482036
user3482036

Reputation: 109

Allow entries only every 7 days. (On Every Monday)

I have an entry form that allows users to enter a competition.

We run this competition every monday 12.00 midnight. Rule is if the user has entered this week competition, they need to wait for next Monday to be allowed to enter competition again.

date_default_timezone_set('GMT');
$today = date("m.d.y H:i:s");
echo strtotime("last Monday");

This is what has been done now I know it's nothing important but I have two questions:

  1. Why does strtotime("last Monday"); return 1398643200?

  2. How do I do the calculation? (Entries all have DATETIME field.)

Upvotes: 0

Views: 103

Answers (3)

bart
bart

Reputation: 198

try this :

//get today's timestamp
$date_now = date_create("today");

//get past timestamp that you want to compare with
$log_date = date_create($VALUE FROM YOUR DB LAST LOG  OR COMPETITION ENTRY);

//calculate date difference
$difference = date_diff($log_date,$date_now);

$int = intval($difference->format('%a'));
if($int > 6) {
    //6 days as user can enter again on the 7th day
    //let me in;
}else{
    //do not let me in;
}

You can refer date_create and date_diff on official documentation for more information.

Upvotes: 0

supergicko
supergicko

Reputation: 61

1398643200 is the UNIX timestamp for the Last Monday, and that is what strtotime("") usually does. It returns a timestamp. Note: You will get an other Timestamp for Last Monday every week.

You can refer more details about strtotime here.

Upvotes: 1

Imat
Imat

Reputation: 520

You just need to add 7 days to your time. Please see the sample code below

$your_date  = "4/28/2014";
$your_date = strtotime($your_date);
$your_date = strtotime("+7 day", $your_date);
echo date('M d, Y', $your_date);

After validating if the user join the competition, you need to give the user a date of the next monday right? Try using this

echo date("M d, Y", strtotime('next monday'));

Upvotes: 0

Related Questions