Date and Time Questions with Answers

9 practice questions on Date and Time (PHP) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Medium

Internally PHP handles dates and times as

  1. A 'date array' array consisting of the year, month, day, hour, minute, and second.
  2. A UNIX time stamp representing the number of seconds since the UNIX epoch.
  3. An ISO-8601 date entity.
  4. A UNIX time stamp consisting of the number of microseconds since the UNIX epoch.

Answer: B. A UNIX time stamp representing the number of seconds since the UNIX epoch.

Explanation

PHP stores all its dates internally as UNIX time stamps, which are defined as the number of seconds since the UNIX epoch, January 1,
1970 00:00:00 UTC.

Step-by-step solution

  1. The question asks how PHP stores dates and times internally.
  2. The explanation says PHP stores all dates as UNIX timestamps, the seconds since 1 January 1970 UTC.
  3. So option B, a UNIX timestamp counted in seconds, is the answer.

Why the other options are wrong

  • A. PHP does not store dates internally as a date array.
  • C. ISO-8601 is a text format, not how PHP stores dates internally.
  • D. The timestamp counts seconds, not microseconds.

Tip: PHP stores one number, the timestamp in seconds, and converts it for display.

Question 2Medium

The ... function parses an English textual date or time into a Unix timestamp

  1. strtodate()
  2. stroftime()
  3. strtotime()
  4. str_to_time()

Answer: C. strtotime()

Explanation

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in 'now', or the current time if 'now' is not supplied.

Step-by-step solution

  1. strtotime() converts English date strings to Unix timestamps.
  2. It parses dates like 'January 1, 2020' into seconds since 1970.
  3. The function returns the corresponding Unix timestamp value.
  4. This matches the correct answer for parsing dates into timestamps.
  5. Final Answer: strtotime()

Why the other options are wrong

  • A. strtodate() is not a PHP function; the English date parser is strtotime().
  • B. stroftime() is not a PHP function; strtotime() parses English dates.
  • D. str_to_time() is not a PHP function; strtotime() parses English dates.

Tip: Remember strtotime() for converting dates to timestamps in PHP.

Question 3Medium

.... Formats a local time or date according to locale settings.

  1. strftime
  2. strgtime
  3. strhtime
  4. stritime

Answer: A. strftime

Explanation

strftime() Format a local time/date according to locale settings
Syntax:

string strftime ( string $format [, int $timestamp = time() ] )

Step-by-step solution

  1. Strftime formats local time or date using locale settings.
  2. It takes a format string and optional timestamp as arguments.
  3. Strgtime is not a valid function name for this purpose.
  4. Strhtime and stritime are also incorrect function names.
  5. The correct function is strftime.

Why the other options are wrong

  • B. strgtime is not a PHP function; the formatting function is strftime.
  • C. strhtime is not a PHP function; the formatting function is strftime.
  • D. stritime is not a PHP function; the formatting function is strftime.

Tip: Remember 'strftime' for formatting local time/date with locale settings.

Question 4Medium

.... Returns the time of sunrise for a given day / location

  1. datesunrise()
  2. date_sunrise()
  3. date-sunrise()
  4. date.sunrise()

Answer: B. date_sunrise()

Explanation

date_sunrise() returns the sunrise time for a given day (specified as a timestamp) and location.

Step-by-step solution

  1. date_sunrise() is the correct function name with no space.
  2. Other options have incorrect syntax like spaces or hyphens.
  3. The function requires a timestamp for the day and location data.
  4. Correct format matches the required parameters for sunrise calculation.
  5. No other option follows the standard naming convention for such functions.
  6. Final answer: date_sunrise()

Why the other options are wrong

  • A. datesunrise() has no underscore, so it does not match the PHP function name date_sunrise().
  • C. date-sunrise() uses a hyphen, which is not valid in a PHP function name.
  • D. date.sunrise() uses a dot, which is not valid in a PHP function name.

Tip: Use camelCase for function names without spaces.

Question 5Medium

The getdate() function returns

  1. An integer
  2. A floating-point number
  3. An array
  4. A string

Answer: C. An array

Explanation

Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given.
Syntax: array getdate ([ int $timestamp = time() ] )

Step-by-step solution

  1. getdate() takes an optional timestamp and uses the current time when none is given.
  2. The explanation says it returns an associative array containing the date information.
  3. So the return value is an array, which is option C.

Why the other options are wrong

  • A. It returns an array of date parts, not a single integer.
  • B. It returns an array, not a floating-point number.
  • D. It returns an array, not a string.

Tip: Read the syntax line: the return type is shown before the function name.

Question 6Medium

....Checks a date for numeric validity.

  1. check_date
  2. verifydate
  3. Verify_date
  4. checkdate

Answer: D. checkdate

Explanation

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined.
Syntax: bool checkdate ( int $month, int $day, int $year )

Step-by-step solution

  1. Checkdate is a function that validates dates by checking month, day, and year.
  2. It returns true if the date is valid, false otherwise.
  3. The correct function name must match exactly as defined in the syntax.
  4. Syntax shows checkdate with lowercase 'd', so it's the correct option.
  5. Only option D matches the required function name exactly.

Why the other options are wrong

  • A. check_date is not a PHP function; the real name is checkdate().
  • B. verifydate is not a PHP function; the real name is checkdate().
  • C. Verify_date has the wrong name; the function is checkdate(), with no underscore.

Tip: Look for exact match of function names in options.

Question 7Medium

You must make a call to ... to specify what time zone you want calculations to take place in before calling any date functions.

  1. date_default_timezone_set()
  2. datedefault_timezone_set()
  3. date_defaulttimezone_set()
  4. date_default_timezoneset()

Answer: A. date_default_timezone_set()

Explanation

date_default_timezone_set() Sets the default timezone used by all date/time functions in a script.

Step-by-step solution

  1. Call date_default_timezone_set() to set the time zone.
  2. This function must be called before any date functions.
  3. Correct syntax is date_default_timezone_set().
  4. Other options have incorrect spelling or formatting.
  5. Final answer: date_default_timezone_set()

Why the other options are wrong

  • B. datedefault_timezone_set() is misspelled; the function is date_default_timezone_set().
  • C. date_defaulttimezone_set() is misspelled; an underscore is needed between default and timezone.
  • D. date_default_timezoneset() is misspelled; the set part needs an underscore before it.

Tip: Always call date_default_timezone_set() before using date functions.

Question 8Medium

What is the difference, in seconds, between the current timestamp in the GMT time zone and the current timestamp in your local time zone?

  1. It depends on the number of hours between the local time zone and GMT
  2. There is no difference
  3. The two will only match if the local time zone is GMT
  4. The two will never match

Answer: B. There is no difference

Explanation

The current timestamp in the GMT time zone and the current timestamp in your local time zone are same.

Step-by-step solution

  1. A Unix timestamp counts seconds since 1 January 1970 00:00:00 UTC.
  2. The current timestamp is the same number of seconds whatever the time zone.
  3. So there is no difference between the GMT and local timestamps, which is option B.

Why the other options are wrong

  • A. The timestamp does not depend on the hour offset, because it counts from UTC.
  • C. The two always match, not only when the local zone is GMT.
  • D. The two do match, so they never match is wrong.

Tip: A time zone changes how a timestamp is displayed, not the timestamp itself.

Question 9Medium

What will the following script output?

< ?php
$time = strtotime ('2004/01/01');
echo date ('H:\i:s', $time);
? >
  1. 00:00:00
  2. 12:00:00
  3. 00:i:00
  4. 12:i:00

Answer: C. 00:i:00

Explanation

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
Syntax: int strtotime ( string $time [, int $now = time() ] )

Step-by-step solution

  1. Strtotime turns the string 2004/01/01 into a timestamp for midnight of that day.
  2. In the date format, the backslash before i makes the letter i print as plain text.
  3. H gives 00 at midnight, the middle part is the literal letter i, and s gives 00.
  4. The output is 00:i:00; the correct answer is option C.

Why the other options are wrong

  • A. The backslash makes i print as a letter, so the middle part is not 00.
  • B. At midnight the hour is 00, not 12, and the middle part is not a number.
  • D. At midnight the hour is 00, not 12, and the middle part is the literal i.

Tip: In date formats, a backslash before a letter prints that letter as plain text.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all PHP topics · All topics