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
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
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
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.
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
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?
<?php
$date = new DateTime();
echo $date->format('l,F,js,Y');
?>
a) Sunday, February 24th 2008
b) Sunday, 02 24 2008
c) Sunday, 24 02 2008
d) Sunday, 24th February 2008
View Answer
Explanation: 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’);
View Answer
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()
a) Only i)
b) Only ii)
c) i) and iv)
d) iii) and iv)
View Answer
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
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
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
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.
- Check PHP Books
- Practice Programming MCQs
- Check MCA Books
- Check Information Technology Books
- Apply for Programming Internship