Q. I would like to know what the current date or time is via my php script? Can you provide me the sample php code?
A. 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
[b] date : Format a local time/date
Examples
Consider following simple example:
<?php
print strftime('%c');
?>Output:
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";
?>Output:
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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 9 comments… read them below or add one }
is there any way to print date for next week in advance. how we are gonna calculate it.
very helpful
thanks very useful information
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.
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
nice information
useful script :-)
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.
woww…nic script..its useful :)