PHP Questions & Answers – Date and Timestamp

«
»

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Date and Timestamp”.

1. What will be the output of the following PHP code?

advertisement
  1.     <?php
  2.     echo (checkdate(4,31,2010) ? 'Valid' : 'Invalid');
  3.     ?>

a) TRUE
b) FALSE
c) Valid
d) Invalid
View Answer

Answer: d
Explanation: The function checkdate() is used to validate a Gregorian date. In the program, April has 30 days and the above date is 31 therefore Invalid is returned.
advertisement
advertisement

2. The date() function returns ___ representation of the current date and/or time.
a) Integer
b) String
c) Boolean
d) Float
View Answer

Answer: b
Explanation: The function date() is used to format a local date and time, and it will return the formatted date string. The syntax of this function is string date(string format [, int timestamp]).

3. Which one of the following format parameter can be used to identify timezone?
a) T
b) N
c) E
d) I
View Answer

Answer: c
Explanation: When the format is E the time zone is identified and returned, for example, America/New York. N denotes ISO-8601 numeric representation of the day of the week, T denotes time zone abbreviation, I denotes whether or not the date is in daylight saving time.
Participate in PHP Programming Certification Contest of the Month Now!
advertisement

4. If the format is F then which one of the following will be returned?
a) Complete text representation of month
b) Day of month, with leading zero
c) Daylight saving time
d) Day of month, without zeros
View Answer

Answer: a
Explanation: F represents a full textual representation of a month, such as January or March. Day of month, with leading zero is represented by D, Daylight saving time by I, Day of month without zeros by j.

5. What will be the output of the following code? (If say date is 22/06/2013.)

advertisement
  1.     <?php
  2.     echo "Today is ".date("F d, Y");
  3.     ?>

a) Today is 22 June, 2013
b) Today is 22-06-2013
c) Today is 06-22-2013
d) Today is June 22, 2013
View Answer

Answer: d
Explanation: The function date() is used to format a local date and time, and it will return the formatted date string. F is the parameter for complete text representation of month, d for day of month, and Y for 4 digit representation of year.
advertisement

6. Which one of the following function is useful for producing a timestamp based on a given date and time?
a) time()
b) mktime()
c) mrtime()
d) mtime()
View Answer

Answer: b
Explanation: The function mktime() returns the Unix timestamp for a date. This function is same as gmmktime() except that the passed parameters represents a date not a GMT date.

7. Which function displays the web page’s most recent modification date?
a) lastmod()
b) getlastmod()
c) last_mod()
d) get_last_mod()
View Answer

Answer: b
Explanation: The function getlastmod() gets the time of the last modification of the main script of execution. It returns the value of the page’s last modified header or FALSE in the case of an error.

8. What will be the output of the following PHP code? (If say date is 22/06/2013.)

  1.     <?php
  2.     printf( date("t") );
  3.     ?>

a) 30
b) 22
c) JUNE
d) 2013
View Answer

Answer: a
Explanation: The t parameter is used to determine the number of days in the current month.

9. Suppose you want to calculate the date 45 days from the present date which one of the following statement will you use?
a) totime(“+45”)
b) totime(“+45 days”)
c) strtotime(“+45 days”)
d) strtotime(“-45 days”)
View Answer

Answer: c
Explanation: The strtotime() function and GNU date syntax is used to calculating the date x days from the present date.

10. To create an object and set the date to JUNE 22, 2013, which one of the following statement should be executed?
a) $date = Date(“22 JUNE 2013”)
b) $date = new Date(“JUNE 22 2013”)
c) $date = DateTime(“22 JUNE 2013”)
d) $date = new DateTime(“22 JUNE 2013”)
View Answer

Answer: d
Explanation: The dateTime() method is class constructor. You can set the date either at the time of instantiation or later by using a variety of mutators.

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.

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 & technical discussions at Telegram SanfoundryClasses.