PHP Questions & Answers – Working with Dates

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Working with Dates”.

1. How many methods does the DateTime class have?
a) 8
b) 9
c) 10
d) 11
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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()
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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();
View Answer

Answer: c
Explanation: 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?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     <?php
  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
View Answer

Answer: a
Explanation: The format() method displays the date in same way as standard date function().

advertisement

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’);
View Answer

Answer: b
Explanation: 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()
advertisement

a) Only i)
b) Only ii)
c) i) and iv)
d) iii) and iv)
View Answer

Answer: c
Explanation: 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)
View Answer

Answer: c
Explanation: 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’);
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: b
Explanation: This outputs a multidimensional array listing past and future changes to the offset from UTC for a DateTimeZone object.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.