# How to use the PHP date format for dates and time

With the PHP date() function, you can **dynamically display both date and time** in nearly any preferred format. This proves particularly useful for presenting publication dates, creating countdown timers, and handling various time-related information.

## What is the PHP date format?

PHP date is a built-in function for **formatting dates and times**. It works by producing a string that represents the date and time, depending on the parameters you specify. For example, you can use the date() function to display current or future dates and times on a web page. If you [retrieve information from a MySQL database using PHP](https://www.ionos.co.uk/digitalguide/websites/web-development/use-php-to-retrieve-information-from-a-mysql-mariadb-database/), date() can convert it into a readable format. This is useful for blog posts or news articles.

Tip [Deploy Now](https://www.ionos.co.uk/hosting/deploy-now "Deploy Now from IONOS") from IONOS optimises both your hosting and development needs. Whether you aim to build a static website, set up an e-commerce shop, or create a blog platform, Deploy Now provides swift setup and adaptable resources tailored to your specific needs.

## This is the syntax of the date() function in PHP

The syntax of the PHP date() function is as follows:

```php
date(format, timestamp);
```

## Which parameters does PHP date accept?

The date() function has one mandatory and one optional parameter.

- **format**: PHP date format is a string parameter that specifies how the date and time should be formatted
- **Y**: represents the year in 4 digits (e.g. 2023)
- **m**: the month as a number starting at zero (01 to 12)
- **d**: the day of the month as a number starting at zero (01 to 31)
- **H**: the hour in a 24-hour format (00 to 23)
- **i**: the minute (00 to 59)
- **s**: the second (00 to 59)
- **timestamp**: This parameter is optional and should be provided as an integer timestamp. If you choose not to include this parameter, the function will default to using the current system time.

A **UNIX timestamp** (also known as a POSIX timestamp) is a simple integer that represents the number of seconds since January 1, 1970 at 00:00:00 UTC (Coordinated Universal Time). This timestamp is a common standard in computer science as it is independent of time zones and makes it possible to perform simple calculations using time information.

To learn more about PHP programming, take a look at our [PHP tutorial](https://www.ionos.co.uk/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/). Check out the advantages and disadvantages of [PHP vs Python](https://www.ionos.co.uk/digitalguide/websites/web-development/php-vs-python/) and [PHP vs JavaScript](https://www.ionos.co.uk/digitalguide/websites/web-development/php-vs-javascript/).

## Examples for using the PHP date() function

The PHP date() function is a powerful tool for formatting dates and times. It can be used in combination with [PHP classes](https://www.ionos.co.uk/digitalguide/websites/web-development/php-classes/), operators and functions to achieve **comprehensive functionality** in your web application.

### Concatenate date and time

You can use [PHP operators](https://www.ionos.co.uk/digitalguide/websites/web-development/php-operators/) such as the **concatenation operator** to combine date and time values with other strings or variables.

```php
$today = date('Y-m-d'); // current year, month and day
$message = 'The current date is: ' . $today;
echo $message;
```

### Generate and customise timestamps

Strtotime() is one of the [PHP functions](https://www.ionos.co.uk/digitalguide/websites/web-development/php-functions/) allowing you to generate a **timestamp** from a string that identifies a date or time. You can then format this timestamp using the date() function.

```php
$dateString = '2023-08-19';
$timestamp = strtotime($dateString);
$formattedDate = date('l, F j, Y', $timestamp);
echo $formattedDate;
```

The result is:

```php
Saturday, August 19, 2023
```

### The PHP DateTime class

The DateTime class represents an **object-oriented method** for working with date and time values, the functionality of which is analogous to the date() function. Here is an example:

```php
$now = new DateTime();
$formattedDate = $now->format('l, F j, Y H:i:s');
echo $formattedDate;
```

First, we create a DateTime object called $now that represents the current date and time. Then we use the **format() method** of the PHP DateTime object to display the date and time in the desired format. Finally, we output the formatted date.


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/websites/web-development/php-date/](https://www.ionos.co.uk/digitalguide/websites/web-development/php-date/) for AI/LLM consumption.