Formatting Dates and Times in PHP: A Beginner's Guide


When developing a website with PHP, it’s often beneficial to report server times and dates.
On its own, this ability is quite useful, but when combined with further scripting, it allows you to account for time zones, datelines, and other user-specific needs.
This improves the overall user experience. For example, popular forum software like vBulletin supports these functions natively.

Best of all, you can output the current server date and time with a simple PHP instruction:



Example: Displaying the Current Date


The following example demonstrates how to echo the current date:

PHP Code
  1. <?php
  2. $r = date("m/d/y"); // Returns: 03/21/2007
  3. echo $r; // Displays the date
  4. ?>




PHP Date and Time Formatting Codes


PHP provides a variety of formatting codes to customize the date and time output. Below is a list of common formatting options:

  • A: Uppercase 12-hour time syntax (e.g., PM)
  • a: Lowercase 12-hour time syntax (e.g., pm)
  • D: Day abbreviation (e.g., Fri)
  • d: Day of the month (e.g., 03)
  • F: Full text month (e.g., January)
  • G: Hours in 24-hour format without leading zeros (e.g., 17)
  • g: Hours in 12-hour format without leading zeros (e.g., 7)
  • H: Hours in 24-hour format with leading zeros (e.g., 17)
  • h: Hours in 12-hour format with leading zeros (e.g., 07)
  • i: Minutes (e.g., 29)
  • M: Month abbreviation (e.g., Jan)
  • m: Month with leading zeros (e.g., 01)
  • O: GMT time difference in hours (e.g., +0400)
  • S: Day of month suffix (e.g., st, nd, rd, th)
  • s: Seconds (e.g., 28)
  • T: Timezone abbreviation (e.g., GMT, PST, EST)
  • t: Total number of days in the month (e.g., 28, 31)
  • U: Seconds since the UNIX epoch (e.g., 1041604168)
  • W: Week number in the year (e.g., 42)
  • w: Numeric representation of the day (e.g., 1 = Monday, 2 = Tuesday)
  • Y: Full year (e.g., 2003)
  • y: Two-digit year (e.g., 03)
  • z: Day of the year (e.g., 002)




Simple Date and Time Codes


Here are a few examples of using the `date()` function to format dates and times:

PHP Code
  1. <?php
  2. date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
  3. date("m.d.y"); // 03.10.01
  4. date("j, n, Y"); // 10, 3, 2001
  5. date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 PST 2001
  6. date("H:i:s"); // 17:16:17
  7. date('it is the jS day.'); // It is the 10th day.
  8. ?>




Output Examples


The above codes would return the following:


- March 10, 2001, 5:16 pm
- 03.10.01
- 10, 3, 2001
- Sat Mar 10 15:16:08 PST 2001
- 17:16:17
- It is the 10th day.



Note: Remember to escape normal characters (e.g., letters or symbols) using the symbol, so they are not interpreted as part of the date/time syntax.



Conclusion


Using PHP's date() function, you can easily display and format server times and dates.
Whether you’re building a basic website or a complex application, these tools allow you to create a better user experience by handling time zones and formatting elegantly.

Experiment with the codes above to suit your needs!
ShadowMage's Avatar
Author:
Views:
2,660
Rating:
Posted on Sunday 22nd December 2024 at 10:48 PM
DanielXP
DanielXP's Avatar
Updated the formatting for this
Posted on Tuesday 26th February 2008 at 07:50 PM
ShadowMage
ShadowMage's Avatar
Yes there is. you could use set_default_timezone('3 DIGIT TIMEZONE CODE'); ;)
Posted on Tuesday 26th February 2008 at 01:32 PM
Nathan
Nathan's Avatar
is there anyway you could make it in california time if i lived in australia?
Posted on Tuesday 27th March 2007 at 04:30 PM
MOD-Dan
MOD-Dan's Avatar
you could always do:
Code
<?PHP
$date = date('jS');
echo("It is the $date day.);
?>