PHP Get Current Date & Time – Full Guide

In PHP, you can use the date() function to retrieve the current date and time. Here are some commonly used date and time formats:

  1. The format Y-m-d H:i:s represents the date and time in the following order: Year-Month-Day Hour:Minute:Second. For example: 2021-01-01 12:00:00.
  2. Year/Month/Day Hour:Minute:Second, for example: 2021/01/01 12:00:00.
  3. Date and time in the format Y年m月d日 H时i分s秒, example: 2021年01月01日 12时00分00秒.

Here is an example code demonstrating how to retrieve the current date and time and format it for output:

$currentDateTime = date("Y-m-d H:i:s");
echo $currentDateTime;

The output result will be similar to: 2021-01-01 12:00:00

Please note that the date() function in PHP defaults to the server’s time zone setting. If you need to use a different time zone, you can use the date_default_timezone_set() function to set the time zone. For example, to set the time zone to Eastern Time in New York.

date_default_timezone_set("America/New_York");
$currentDateTime = date("Y-m-d H:i:s");
echo $currentDateTime;

The output will be the current date and time in the corresponding time zone.

bannerAds