PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP date(): Date Time Formatting

The date() function in PHP is a built-in function that is used to format a local date and time, and return the formatted date string. It provides a number of different options to display date and time in various formats.

Here is the syntax of date() function:

date(format, timestamp)
  • format: Required. Specifies the format of the outputted date string.
  • timestamp: Optional. Specifies a timestamp. Default is the current date and time.

Here's an example of a simple use of the date() function:

echo date('Y-m-d');

This will output the current date in the format "YYYY-MM-DD".

Here are some commonly used format options:

  • d: Day of the month, 2 digits with leading zeros (01 to 31)
  • D: A textual representation of a day, three letters (Mon through Sun)
  • j: Day of the month without leading zeros (1 to 31)
  • l (lowercase 'L'): A full textual representation of the day of the week (Sunday through Saturday)
  • m: Numeric representation of a month, with leading zeros (01 through 12)
  • M: A short textual representation of a month, three letters (Jan through Dec)
  • n: Numeric representation of a month, without leading zeros (1 through 12)
  • F: A full textual representation of a month, such as January or March
  • Y: A full numeric representation of a year, 4 digits (Examples: 1999 or 2003)
  • y: A two digit representation of a year (Examples: 99 or 03)

You can use these format options in combination to display the date and time in almost any way you want. Here's an example:

echo date('l, F j, Y');

This will output the current date in the format "Monday, January 1, 2023".

You can also use the date() function with a specific timestamp:

$timestamp = strtotime('2023-12-31');
echo date('Y-m-d', $timestamp);

This will output "2023-12-31". The strtotime() function is used to convert a date/time string to a Unix timestamp.

  1. How to Format Current Date and Time in PHP:

    Use date() to format the current date and time.

    <?php
    $formattedDateTime = date('Y-m-d H:i:s');
    echo $formattedDateTime;
    
  2. Formatting PHP Date and Time with Custom Patterns:

    Create custom date and time patterns.

    <?php
    $customFormat = date('l, F jS Y \a\t h:i A');
    echo $customFormat;
    
  3. Using date() for Displaying Day, Month, and Year in PHP:

    Format individual components of the date.

    <?php
    $day = date('d');
    $month = date('m');
    $year = date('Y');
    
    echo "Today is $day/$month/$year";
    
  4. PHP date() for Displaying Time in 12-hour and 24-hour Format:

    Format time in both 12-hour and 24-hour formats.

    <?php
    $time12Hour = date('h:i A');
    $time24Hour = date('H:i');
    
    echo "12-Hour Format: $time12Hour, 24-Hour Format: $time24Hour";
    
  5. Formatting Date and Time with Timezones in PHP:

    Specify a timezone when formatting date and time.

    <?php
    date_default_timezone_set('America/New_York');
    $formattedDateTime = date('Y-m-d H:i:s');
    
    echo "Current date and time in New York: $formattedDateTime";
    
  6. PHP date() Function for Outputting Timestamps:

    Convert timestamps to formatted date and time.

    <?php
    $timestamp = time();
    $formattedDateTime = date('Y-m-d H:i:s', $timestamp);
    
    echo "Formatted date and time: $formattedDateTime";
    
  7. Displaying Day Names and Month Names with date() in PHP:

    Retrieve and display day and month names.

    <?php
    $dayName = date('l');
    $monthName = date('F');
    
    echo "Today is $dayName, $monthName";
    
  8. Customizing Date Separators in PHP date() Output:

    Customize date separators in the formatted output.

    <?php
    $customFormat = date('Y|n|j');
    echo $customFormat;
    
  9. PHP date() for Formatting Dates in Different Locales:

    Format dates using different locales.

    <?php
    setlocale(LC_TIME, 'fr_FR'); // French locale
    $formattedDate = strftime('%A %d %B %Y');
    echo $formattedDate;
    
  10. Formatting Dates with strtotime() and date() in PHP:

    Use strtotime() to parse date strings and format with date().

    <?php
    $futureDate = strtotime('+1 week');
    $formattedFutureDate = date('Y-m-d', $futureDate);
    
    echo "One week from today: $formattedFutureDate";
    
  11. PHP date() Formatting for Relative Time Expressions:

    Display relative time expressions using date().

    <?php
    $timestamp = strtotime('yesterday');
    $relativeTime = date('l', $timestamp);
    
    echo "Yesterday was $relativeTime";
    
  12. Handling Date and Time Arithmetic with PHP date() Function:

    Perform date and time arithmetic using date().

    <?php
    $nextWeek = strtotime('+1 week');
    $formattedNextWeek = date('Y-m-d', $nextWeek);
    
    echo "One week from today: $formattedNextWeek";
    
  13. PHP date() and strftime() for Localized Date Formatting:

    Use strftime() for more localized date formatting.

    <?php
    setlocale(LC_TIME, 'de_DE'); // German locale
    $localizedDate = strftime('%A %d %B %Y');
    
    echo "Localized date: $localizedDate";