Share and save

Share/Save

How to display date and time?

How does one display the current date and time on a Drupal site?

Question answer: 

Time for this newbie to take the plunge, and touch... oh, sweet mother of mercy... touch PHP!

A Drupal.org forum visitor asked about displaying the date and time on a site. I know there are countless ways to do this using web technologies, including any number of "widgets" available out there. For all I know, there may be a Drupal module that tosses up a block or something, too.

But I know this is the sort of task one can also do easily using PHP, the real guts of Drupal (well, part of the guts, anyway... maybe the pancreas). And it should be easy, too. So as a farewell to my... ahem... chastity with regards to PHP, let me take this oh-so-newbie challenge upon myself.

Google turns up all kinds of stuff. Like this couldn't-be-more-basic, one-size solution: http://www.totallyphp.co.uk/code/display_date_and_time_with_php.htm

Besides some code comments, there's just one line of key code there:

<?php  echo date("l, F d, Y h:i" ,time()); ?>

Let's try! I make a new block... turn off rich-text input (TinyMCE)... select PHP as my input format... and paste in the code. I save the block and make it display... and there it is, to the left! Stupendous!

Now, that's the barest of starts; I'll add more to this page as I play with more formatting and other options. (Like time zones: that's pretty important!) But wow, I'm now PHP-experienced and it didn't hurt at all. Before long I'll be up for CSS/HTML/PHP themeing three-ways til the break of dawn. Mmm, break out the mango love butter.

Share/Save
Unknown Drupaloid's picture

Thank you!

Thanks for this. I was once afraid. Now, less so!

Bill Shaw's picture

Re: How to display date and time?

Works beautifully.

I shortened up the code to only display the date, until I fugre how to account for time zones. But until then, this gets me there ;)

Thanks!

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><br><p>
  • You may quote other posts using [quote] tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options


Drupal power!

Powered by Drupal, an open source content management system

Drupal mini tip

An obvious but important tip: When installing a new module, be sure to check for READ ME files or other instructions! It's easy to forget that not all modules are simple "plug and play"; some require special preparation, such as modifications to the site's Theme, to work their magic.

Remember, modules often make changes to your ste's database, its very "DNA". Take their installation seriously: read modules' instructions, and back up the database before installation too.