PHP Date and Time Questions and Answers with Useful Programs

Related Topics:

PHP Programming : PHP Date and Time

PHP Date and Time Multiple Choice Questions and Answers with Useful Programs and Explanations.
1. How many php methods are available in DateTime Class?
[A] 12
[B] 6
[C] 3
[D] 9

Answer: A. 12

Explanation:

DateTime class have nine methods as below:

DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
    DateTime::__construct — Returns new DateTime object
    DateTime::createFromFormat — Returns new DateTime object formatted according to the specified format
    DateTime::getLastErrors — Returns the warnings and errors
    DateTime::modify — Alters the timestamp
    DateTime::__set_state — The __set_state handler
    DateTime::setDate — Sets the date
    DateTime::setISODate — Sets the ISO date
    DateTime::setTime — Sets the time
    DateTime::setTimestamp — Sets the date and time based on an Unix timestamp
    DateTime::setTimezone — Sets the time zone for the DateTime object
    DateTime::sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
2. How many constants does the DateTime class have in php 5.6?
[A] 8
[B] 9
[C] 10
[D] 11

Answer: D. 11

Explanation:

Nine constants are available in DateTime class in php 5.6 are as below:

const ATOM = 'Y-m-d\TH:i:sP'; 
const COOKIE = 'l, d-M-y H:i:s T';
const ISO8601 = 'Y-m-d\TH:i:sO';
const RFC822 = 'D, d M y H:i:s O';
const RFC850 = 'l, d-M-y H:i:s T';
const RFC1036 = 'D, d M y H:i:s O';
const RFC1123 = 'D, d M Y H:i:s O';
const RFC2822 = 'D, d M Y H:i:s O';
const RFC3339 = 'Y-m-d\TH:i:sP';
const RSS = 'D, d M Y H:i:s O';
const W3C = 'Y-m-d\TH:i:sP';
3. Which one of the following format parameter is correct to get timezone in date() function?
[A] F
[B] T
[C] e
[D] D

Answer: C. e

Explanation:


echo date('e');

Timezone identifier (added in PHP 5.1.0) 
Examples: UTC, GMT, Atlantic/Azores
4. Which one of the following format parameter can be used to get seconds with leading zero?
[A] S
[B] d
[C] Y
[D] s

Answer: D. s

Explanation:


echo date('s');

It will return current seconds with leading zero 00 through 59.
5. How we can set default timezone for our PHP application or website?
[A] set_time_zone('UTC');
[B] default_date_timezone_set('UTC');
[C] date_default_timezone_set('UTC');
[D] default_timezone('UTC')

Answer: C. date_default_timezone_set('UTC');

Explanation:

date_default_timezone_set('UTC'); is used to set the default timezone to use. Available since PHP 5.1
6. What will be the output of following PHP snippet?
<?php 
      echo  date('\i\t \i\s \t\h\e jS \d\a\y.');

?>
[A] \i\t \i\s \t\h\e jS \d\a\y.
[B] it is the 10th day.
[C] \i\t \i\s \t\h\e 10th \d\a\y.
[D] it is the 10S day.

Answer: B. it is the 10th day.

7. Which of the following functions do not return a timestamp?
[A] time()
[B] date()
[C] strtotime()
[D] localtime()
[E] gmmktime()

Answer: B. date()

8. ................ Formats a local time or date according to locale settings.
[A] strftime
[B] strgtime
[C] strhtime
[D] stritime

Answer: A. strftime

9. The ......... function parses an English textual date or time into a Unix timestamp
[A] strtodate()
[B] stroftime()
[C] strtotime()
[D] str_to_time()

Answer: C. strtotime()

10. What would happen if the following script were run on a Windows server set to Moscow, Russia’s time zone?

<?php
echo gmmktime(0, 0, 0, 1, 1, 1970);
?>
[A] It would output the number 0
[B] It would output the number -1
[C] It would output the number 1
[D] It would raise an error
[E] It would output nothing

Answer: B. It would output the number -1

PHP Date and Time Programs/Functions


Comments: (Your feedback is valuable to us)

Davinder Singh 1 year ago Reply

Thanks admin ... i really like this... please share more questions

Yogesh 1 year ago

yes its really nice ...


;