250+ TOP MCQs on PHP Working with Dates and Answers

PHP Multiple Choice Questions on “Working with Dates”.

1. How many methods does the DateTime class have?
a) 8
b) 9
c) 10
d) 11
Answer: b
Clarification: The DateTime class has nine methods, all of which are public.

2. How many constants does the DateTime class have?
a) 8
b) 9
c) 10
d) 11
Answer: d
Clarification: The DateTime class has 11 constants, no static properties or methods.

3. Which method is simply an object-oriented version of date()?
a) DateTime::format()
b) DateTime::modify()
c) DateTime::setTime()
d) DateTime::setDate()
Answer: a
Clarification: The format() method is simply an object-oriented version of date(). It takes $dateFormat as an argument. $dateFormat is a string consisting of the same date formatting characters accepted by the procedural date() function.

4. Which of the following is the right way to use the DateTime class?
a) $date = get_Class(DateTime);
b) $date = class DateTime;
c) $date = new DateTime();
d) $date = new class DateTime();
Answer: c
Clarification: The way you use the DateTime class is like any other class: instantiate an object, and store it in a variable.

5. What will be the output of the following PHP code if date is 24/02/2008?

  1.     
  2.     $date = new DateTime();
  3.     echo $date->format('l,F,js,Y');
  4.     ?>

a) Sunday, February 24th 2008
b) Sunday, 02 24 2008
c) Sunday, 24 02 2008
d) Sunday, 24th February 2008
Answer: a
Clarification: The format() method displays the date in same way as standard date function().

6. Which of the following statements can be used to set the time zone in individual scripts?
a) date_set_timezone(‘Europe/London’);
b) date_default_timezone_set(‘Europe/London’);
c) date_set_default_timezone(‘Europe/London’);
d) date_default_timezone(‘Europe/London’);

Answer: b
Clarification: The function date_default_timezone_set is used to set the default time zone used by all date/time functions in a script. You can also use ini_set(‘date.timezone’, ‘Europe/London’);

7. Which of the following DateTimeZone classes are static?

i)  listAbbreviations()
ii) getName()
iii) getOffset()
iv) listIdentifiers()

a) Only i)
b) Only ii)
c) i) and iv)
d) iii) and iv)
Answer: c
Clarification: listAbbreviations() and listIdentifiers() are static methods.

8. Which of the following DateTimeZone classes are non-static?

i) _construct()
ii) getName()
iii) getOffset()
iv) getTransitions()

a) Only i)
b) Only ii)
c) i), ii), iii) and iv)
d) iii) and iv)
Answer: c
Clarification: All of the given methods are non static.

9. Which of the following statements can be used to add two months to the existing date?
a) $date->modify(‘+2 months’);
b) $date = modify(‘+2 months’);
c) $date = modify(‘2+ months’);
d) $date->modify(‘2+ months’);
Answer: a
Clarification: To change the date stored by a DateTime object after it has been created, you use DateTime::modify() with a natural language expression.

10. Which method enables you to calculate whether daylight saving time is in force at a specific date and time?
a) getOffset()
b) getTranitions()
c) ISODate()
d) savingTime()
Answer: b
Clarification: This outputs a multidimensional array listing past and future changes to the offset from UTC for a DateTimeZone object.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top