php css vector image

How to Get Current Time and Date in PHP

Getting the current time and date in PHP is easy. The most commonly used function for this is date(), which allows you to format the output however you want.

Basic Usage of date()

<?php
echo date("Y-m-d H:i:s");
// Example output: 2025-05-02 15:30:45
?>

In this example, "Y-m-d H:i:s" formats the output as year-month-day hours:minutes:seconds.

Different Format Examples

  • Full Date: date("F j, Y") → April 2, 2025
  • Time Only: date("h:i A") → 03:30 PM
  • Day and Date: date("l, d M Y") → Friday, 02 May 2025

Using time() with date()

Although date() automatically uses the current time, you can explicitly pass time() for clarity:

<?php
echo date("Y-m-d H:i:s", time());
?>

Set Timezone (Important)

PHP uses the server’s timezone by default. To avoid confusion, it’s a good idea to set the timezone manually:

<?php
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d H:i:s");
?>

This ensures you're seeing the correct time for your location.