PHP Date and Time Flashcards

1
Q

Q: What function is used to get the current timestamp in PHP?

A

A: time().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What does the time() function return?

A

A: The current Unix timestamp (seconds since January 1, 1970).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: How do you format a date in PHP?

A

A: Use the date() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What is the syntax of the date() function?

A

A: date(format, timestamp);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: What is the default time zone in PHP?

A

A: It depends on the server configuration, often UTC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: How do you format a date as “Year-Month-Day”?

A

A: date(‘Y-m-d’);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What does the format character d represent in the date() function?

A

A: Day of the month (01–31).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: What does the format character m represent in the date() function?

A

A: Numeric representation of a month (01–12).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What does the format character Y represent in the date() function?

A

A: A four-digit year.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: What does the format character l represent in the date() function?

A

A: Full name of the day of the week (e.g., “Monday”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What does the format character H represent?

A

A: Hour in 24-hour format (00–23).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What does the format character h represent?

A

A: Hour in 12-hour format (01–12).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: What does the format character i represent?

A

A: Minutes (00–59).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What does the format character s represent?

A

A: Seconds (00–59).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: How do you display the current time as HH:MM:SS?

A

A: date(‘H:i:s’);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: How do you set the default time zone in PHP?

A

A: date_default_timezone_set(‘timezone’);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: How do you get the current default time zone?

A

A: date_default_timezone_get();.

18
Q

Q: How do you list all supported time zones in PHP?

A

A: Use the timezone_identifiers_list() function.

19
Q

Q: What is the default time zone in a PHP installation if not configured?

A

A: UTC.

20
Q

Q: How do you format a date for a specific time zone?

A

A: Use the DateTime class with the setTimezone() method.

21
Q

Q: How do you get the timestamp for a specific date and time?

A

A: Use the mktime() function.

22
Q

Q: What does the mktime() function return?

A

A: The Unix timestamp for the specified date and time.

23
Q

Q: What function converts a timestamp into a formatted date?

A

A: date().

24
Q

Q: How do you convert a formatted date string to a timestamp?

A

A: Use the strtotime() function.

25
Q

Q: What does the strtotime() function do?

A

A: Parses a date string into a Unix timestamp.

26
Q

Q: What is the DateTime class in PHP?

A

A: A class for working with dates and times in an object-oriented way.

27
Q

Q: How do you create a new DateTime object?

A

A: $date = new DateTime();.

28
Q

Q: How do you format a DateTime object?

A

A: Use the format() method, e.g., $date->format(‘Y-m-d H:i:s’);.

29
Q

Q: How do you set the time zone for a DateTime object?

A

A: Use the setTimezone() method.

30
Q

Q: How do you add or subtract time from a DateTime object?

A

A: Use the add() or sub() method with a DateInterval object.

31
Q

Q: What is the DateInterval class in PHP?

A

A: A class that represents a time interval.

32
Q

Q: How do you create a DateInterval object?

A

A: new DateInterval(‘P1D’); (adds one day).

33
Q

Q: What does the P in DateInterval mean?

A

A: Period; used to define the duration.

34
Q

Q: How do you add one month to a date?

A

$date->add(new DateInterval(‘P1M’));

35
Q

Q: Can you use negative intervals with DateInterval?

A

A: Yes, by prefixing with a -, e.g., P-1D.

36
Q

Q: How do you calculate the difference between two dates?

A

A: Use the diff() method of the DateTime class.

37
Q

Q: What does the diff() method return?

A

A: A DateInterval object.

38
Q

Q: How do you get the difference in days between two dates?

A

A: $diff->days.

39
Q

Q: How do you check if a date is in the past?

A

A: Compare the timestamp with time().

40
Q

Q: How do you determine if a year is a leap year in PHP?

A

A: Check if date(‘L’, strtotime(‘YYYY-01-01’)) returns 1.