Bird
0
0

You want to set a cookie that expires exactly at midnight today (server time). Which code snippet correctly sets this cookie named 'daily'?

hard📝 Application Q8 of 15
PHP - Sessions and Cookies
You want to set a cookie that expires exactly at midnight today (server time). Which code snippet correctly sets this cookie named 'daily'?
Asetcookie('daily', 'yes', time() + 86400);
Bsetcookie('daily', 'yes', strtotime('today midnight'));
Csetcookie('daily', 'yes', mktime(0,0,0));
Dsetcookie('daily', 'yes', strtotime('tomorrow midnight'));
Step-by-Step Solution
Solution:
  1. Step 1: Understand the meaning of 'midnight today'

    Midnight today means the start of the current day, which is already passed if current time is after midnight.
  2. Step 2: Identify the correct timestamp for expiration

    To expire at the next midnight, use strtotime('tomorrow midnight') which gives midnight of the next day.
  3. Final Answer:

    setcookie('daily', 'yes', strtotime('tomorrow midnight')); -> Option D
  4. Quick Check:

    Expire at next midnight = strtotime('tomorrow midnight') [OK]
Quick Trick: Use strtotime('tomorrow midnight') for next day midnight [OK]
Common Mistakes:
  • Using 'today midnight' which is past
  • Using mktime(0,0,0) without date context
  • Adding 86400 seconds blindly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes