PHP Script To Find and Print The Current Date And Time

I would like to know what the current date or time is via my php based script? Can you provide me the sample php code to display current date and time?

PHP has inbuilt functions to display current date and time.

PHP date/time functions

[a] strftime() : Format a local time/date according to locale settings using php.

[b] date() : Format a local time/date using php.

Examples: PHP show current date and time

Consider the following simple php script example:

<?php
print strftime('%c');
?>

Sample outputs:

Mon Apr 23 01:22:58 2007

You need to pass format such as %c to strftime() to print date and time representation for the current system. You can use following format characters:

  • %m – month as a decimal number (range 01 to 12)
  • %d – day of the month as a decimal number (range 01 to 31)
  • %Y – year as a decimal number including the century
  • You can see the complete format conversion specifiers online here

You can also use date() as follows:

<?php
 print date('r');
 print "\n";
 print date('D, d M Y H:i:s T');
 print "\n";
?>

Sample outputs:

Mon, 23 Apr 2007 01:29:56 +0530
Mon, 23 Apr 2007 01:35:14 IST

You need to pass r format conversion to print formatted date. See the complete format conversion specifiers online here.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.