0

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.)

quamrana
  • 37,849
  • 12
  • 53
  • 71
user3482036
  • 109
  • 11
  • the number that gets returned is the same as time() which are the amount of seconds that passed since a certain date, not sure when this date was, but its a long time ago. – kpp May 01 '14 at 09:11
  • possible duplicate http://stackoverflow.com/questions/4150435/php-strtotime-last-monday-if-today-is-monday – Girish May 01 '14 at 09:11

3 Answers3

1

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.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
supergicko
  • 61
  • 5
0

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'));
Imat
  • 500
  • 2
  • 4
  • 15
  • Close enough but not what I have problem with, so every time a user is entering this competition I need to know if they have already been in this week competition, if not then they can the monday after that – user3482036 May 01 '14 at 09:22
  • so you just need to display the next monday if the user is already join the competetion. This will probably solve your problem. echo date("M d, Y", strtotime('next monday')); – Imat May 01 '14 at 09:31
0

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.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
bart
  • 198
  • 1
  • 8
  • That is the issue, what I mean is that its not based on every 7 day, its based on every monday. So if user enters competition on saturday night they can again enter coming tuesday – user3482036 May 01 '14 at 09:27
  • Well easiest way to use mysql event scheduler or cron to reset date on your DB every monday @ 12:00 job done no calculations needed. – bart May 01 '14 at 09:44