About Linux FAQ

Browse More FAQs:

PHP script to find and print the current date and time

Posted by Vivek on Sunday April 22, 07 @8:06 pm

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

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Linux / UNIX FAQ:

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

~ Last updated on: April 22, 2007

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.