1

I want to retrive a UNIX epoch (as returned by the PHP function time()) and display it as a formatted date as part of a text (i.e. a #markup element) in a form. This is not a field where the user is supposed to enter a date. It is just showing the current date and time in a human readable format inside the form.

Is know that I can get a human readable date and time from a Unix epoch with the PHP date() function.

However, I am looking for the "Drupal" way of doing this. I think this means that I should display the date and time in the short format set by the site manager under Home » Administration » Configuration » Regional and language.

Is there a way to do this (or is there another way that is condidered "the Drupal way")?

Edit: Function format_date() turned out to be the right answer. It depends on a set of format variables (e.g. date_format_short) being set. If this variable is undefined, you only get the US-centric formats hard-coded into format_date(). The confusing bit is that when you visit the Home » Administration » Configuration » Regional and language admin page, it shows you some formats (and I therefore errenously assumed that these formats were those that were defined for the site). But these formats are not defined until you press Save configuration on that page.

3

Sounds like you want to use the format_date() function.

$now = time();
$date = format_date($now, 'short');

You can use it with any of the date formats that are defined in your system, or you can pass in a custom date string.

  • Not sure why that doesn't work; I'm nearly positive I have done this on other sites before. Double check the value of the date_format_short variable and make sure it matches what you think it should be. You can also make a new date type from admin/config/regional/date-time and use that. If you hover over the DELETE link, you will see the machine name to use. – mpdonadio Jan 11 '13 at 13:55
  • Glad you got it fixed. – mpdonadio Jan 11 '13 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.