How to Get Current Date and Time in PHP
Make sure that you have PHP properly installed and running on your computer or server., Open up an existing PHP document, or create a new one., Add a PHP opening and closing statement, so that you may place your PHP code inside. , Call the date()...
Step-by-Step Guide
-
Step 1: Make sure that you have PHP properly installed and running on your computer or server.
The file name should end with ".php" or ".php3".
The two text editors recommended are either Adobe Dreamweaver or Notepad++. ,,, First understand how to get the date and year of your current timezone: echo date ( 'm/d/Y g:ia' ); This will output as: "11/20/2014 8:32am". , Look at the above example.
Inside the date() function, there is a string with the following text: 'm/d/Y g:ia'.
The m stands for the month, d for day, and Y for year.
The g for hour, i for minute, and a for am/pm.
To learn more about what options exist, check out PHP's date() function manual: http://php.net/manual/en/function.date.php , To change the timezone in PHP, use this function: date_default_timezone_set ( 'America/Los_Angeles' ); where 'America/Los_Angeles' refers to the timezone you wish to switch to.
Anytime you call a function (after calling date_default_timezone_set) that interacts with the date/time, you will be using the new selected timezone. -
Step 2: Open up an existing PHP document
-
Step 3: or create a new one.
-
Step 4: Add a PHP opening and closing statement
-
Step 5: so that you may place your PHP code inside.
-
Step 6: Call the date() function from PHP.
-
Step 7: Discuss the formatting options for the date() function.
-
Step 8: Choose the format.
-
Step 9: Select the timezone.
Detailed Guide
The file name should end with ".php" or ".php3".
The two text editors recommended are either Adobe Dreamweaver or Notepad++. ,,, First understand how to get the date and year of your current timezone: echo date ( 'm/d/Y g:ia' ); This will output as: "11/20/2014 8:32am". , Look at the above example.
Inside the date() function, there is a string with the following text: 'm/d/Y g:ia'.
The m stands for the month, d for day, and Y for year.
The g for hour, i for minute, and a for am/pm.
To learn more about what options exist, check out PHP's date() function manual: http://php.net/manual/en/function.date.php , To change the timezone in PHP, use this function: date_default_timezone_set ( 'America/Los_Angeles' ); where 'America/Los_Angeles' refers to the timezone you wish to switch to.
Anytime you call a function (after calling date_default_timezone_set) that interacts with the date/time, you will be using the new selected timezone.
About the Author
George Jones
Experienced content creator specializing in creative arts guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: