PHP date() Function | PHP time() Function
Notes:
PHP Date and Time Functions:
date_default_timezone_set(timeZoneName:string)
- is used to get the date and time relative to local time zone or local date and time
Ex: date_default_timezone_set("Asia/Kolkata");
Note: list of time zone names by country: https://timezonedb.com/time-zones
date(format:string):string
- Returns date and time according to the given string format
Ex: echo date("d/m/Y h:i:s A");
Using string formatting characters:
// get day
echo date("d"); // Returns day of the month (01 to 31) (numeric representation)
echo date("D"); // Returns day of the week (Mon to Sun) (short textual representation)
echo date("l"); // Returns day of the week (Monday to Sunday) (long textual representation)
// get month
echo date("m"); // Returns month of the year (01 to 12) (numeric representation)
echo date("M"); // Returns month of the year (Jan to Dec) (short textual representation)
echo date("F"); // Returns month of the year (January to December) (long textual representation)
// get year
echo date("Y"); // Returns year in 4 digits
echo date("y"); // Returns year in 2 digits
// get hours
echo date("h"); // Returns hours - in 12 hours format
echo date("H"); // Returns hours - in 24 hours format
// get minutes
echo date("i"); // Returns minutes
// get seconds
echo date("s"); // Returns seconds
echo date("a"); // Returns am or pm
echo date("A"); // Returns AM or PM
time(void):int
-returns time in seconds since January 1 1970
Ex: echo time();
Creating date using time method:
Ex: echo date("d/m/Y h:i:s A",time());
Finding Previousand Nextdate:
Ex: echo date("d/m/Y h:i:s A",time() + 1*24*60*60); // Nextdate
cho date("d/m/Y h:i:s A",time() - 1*24*60*60); // Previousdate
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a