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.
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 1m

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
18 comments… add one
  • car sell Jul 12, 2009 @ 18:19

    is there any way to print date for next week in advance. how we are gonna calculate it.

  • soni Feb 11, 2010 @ 11:15

    very helpful

  • pondicherry Jun 14, 2010 @ 15:45

    thanks very useful information

  • dorathi Jun 17, 2010 @ 11:13

    Create a PHP web page which prints the current time in various formats and colors depending on the query string. Your php page is intended to be loaded with a simple query string which controls how the time is displayed, the page title and the background and text colors. The query string has three fields separated by colons. The first is the time format, the second is the text color and the third is the background color.

  • omkar Oct 28, 2010 @ 13:17

    i will design contact us page in php
    in that i will send some information that fill to user. and also send current time and date

    then how can i send current time and date to mail
    i will send date by using date()
    so u will tell me how can i send current time

    thank you

  • govardhan Jan 25, 2011 @ 8:07

    nice information

  • Shobana T Mar 10, 2011 @ 6:03

    useful script :-)

  • Amit Nov 1, 2011 @ 13:21

    I want to print date in php but date should be independent of system date means if I change the system date then it will print the current date from internet like if i change my system date from 1 nov to 23 nov then script will print the date of that day only not the system date 23 nov.

    Please suggest me some solution.
    Thank You in Advance.

  • Jaiveek Mar 7, 2012 @ 10:57

    woww…nic script..its useful :)

  • shiven Jun 8, 2013 @ 12:12

    thanks……………..guys

  • jignesh Jun 30, 2013 @ 9:19

    hey…its not print local time in my pc, what can i do to print local time??

  • nidhi Aug 16, 2013 @ 4:13

    i want to enter some information then save it in datebase and also want to print that information, with only click on one button that is “save & print”

  • ythann Nov 30, 2013 @ 4:24

    i just want to ask if what is the php codes for changing the current time of the computer where the system is

  • Gourav RR Jan 29, 2014 @ 11:17

    Nice….
    It works correctlu…

  • Gourav RR Jan 29, 2014 @ 11:18

    Nice….
    It works correctly…

  • prasenjit datta Feb 13, 2014 @ 12:26

    lovely…bt i cnt see the current system time..what can i do???plzzz hlp me…

  • kiran Mar 12, 2014 @ 17:05

    hey i like php programs b’coz they are simple & short

  • sharad Oct 4, 2016 @ 10:50

    Thank you . .

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.