Skip to content

Instantly share code, notes, and snippets.

@hassan00dev
Last active May 17, 2021 19:54
Show Gist options
  • Select an option

  • Save hassan00dev/89ff58eb69a4ce119ea070ff65239b6b to your computer and use it in GitHub Desktop.

Select an option

Save hassan00dev/89ff58eb69a4ce119ea070ff65239b6b to your computer and use it in GitHub Desktop.
PHP Date and Time Functions:
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
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)
echo data("3"); // Returns day of the week
// 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 previous and next date:
Ex: echo date("d/m/Y h:i:s A",time() + 1*24*60*60); // next date
echo date("d/m/Y h:i:s A",time() - 1*24*60*60); // previous date
date('Y-m-d',strtotime('next Friday')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment