Date function in PHP

In PHP, the date() function is used to format a date and/or time string. Here's an example of how to use the date() function:


<?php
  echo "Today's date is: " . date("Y-m-d"); // outputs "Today's date is: 2023-04-23"
?>

In this example, we're using the date() function to format today's date as a string in the YYYY-MM-DD format. The Y represents the year with four digits, m represents the month with two digits, and d represents the day with two digits.

You can also include the time in the output by adding a time format string as the second parameter to the date() function. For example:


<?php
  echo "The current time is: " . date("h:i:s A"); // outputs "The current time is: 08:12:34 PM"
?>

In this example, we're using the h to represent the hour in 12-hour format with leading zeros, i to represent the minutes with leading zeros, s to represent the seconds with leading zeros, and A to represent the meridian (AM or PM) in uppercase letters.

Comments

Leave a Reply