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()...

9 Steps 1 min read Medium

Step-by-Step Guide

  1. 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.
  2. Step 2: Open up an existing PHP document

  3. Step 3: or create a new one.

  4. Step 4: Add a PHP opening and closing statement

  5. Step 5: so that you may place your PHP code inside.

  6. Step 6: Call the date() function from PHP.

  7. Step 7: Discuss the formatting options for the date() function.

  8. Step 8: Choose the format.

  9. 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

G

George Jones

Experienced content creator specializing in creative arts guides and tutorials.

108 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: