Jump to content

how to format dates in templates?


totoff
 Share

Recommended Posts

hello forum,

i have two date fields both of which i reuse for several templates. as this templates require different output format for dates, i'm trying to format my date in the template code. unfortunately this code (found in this post) returns january 1970 but not the correct date entered in the date field "closing":

<?php echo date("Y-m-d",$page->closing); ?>

however, the same code used for a core variable works and returns the correct date on which the page was created:

<?php echo date("Y-m-d", $news->created); ?>

what am i doing wrong? thanks for your help.

Link to comment
Share on other sites

PHP's date expects a Unix timestamp. $page->closing most likely returns a formatted date that is set in the fields output formate setting. Try:

<?php echo date("Y-m-d", $page->getUnformatted("closing")); ?>

The created and updated system values return unix timestamps by default i think, so this is why they work in your example.

  • Like 7
Link to comment
Share on other sites

@SiNNuT,

if it is already formatted as you suspect, perhaps this would do:

<?php echo $page->closing; ?>

It would, but totoff wants to format $page->closing differently depending on the template he uses it on, or at least that how i interpret his question.

If this is not the case, then of course you could just set the desired output format in the field settings and just do 'echo $page->closing'

  • Like 1
Link to comment
Share on other sites

Hi totoff,

I have also had some issues with dates and formatting. The best solution I have found (especially when you a see a date returning the unix epoch of 1970) is to first convert the date using strtotime, then do the formatting. for example

<?php echo strftime("%d %B %Y", strtotime($page->my_date_field)); ?>

which should output 16 October 2012.

After writing this and thinking, I have a feeling that user created date fields are having some extra formatting added by PW which confuses the extra formatting you are trying to make in the template (while the built-in "created" field is just returning a unix timestamp and works ok), so probably the best solution would be to try $page->getUnformatted('date_field') like SiNNuT suggested.

  • Like 1
Link to comment
Share on other sites

hi all,

thanks for answering to my post. first of all, what sinnut suggested works. and he is right in his understanding of my question: i need the date to be formatted differently depending on the template in which the field is used. otherwise i would have set the format in the field as usual.

however, what surprises me is, that i set the output formatting in the date field to none BEFORE i tried

<?php echo date("Y-m-d",$page->closing); ?>

so i understand, that

<?php echo date("Y-m-d", $page->getUnformatted("closing")); ?>

removes all formatting from the timestamp and gives way for formatting it with date(). but shouldn't i get the same effect/result setting the field output format to none?

Link to comment
Share on other sites

@michael

thanks vey much, you answered while i was writing my post. sinnuts way is definitely the one that works. i agree that there must be some "extra formatting" no matter what the field settings are. otherwise i couldn't explain why my example posted above didn't work ...

Link to comment
Share on other sites

@michael

thanks vey much, you answered while i was writing my post. sinnuts way is definitely the one that works. i agree that there must be some "extra formatting" no matter what the field settings are. otherwise i couldn't explain why my example posted above didn't work ...

Glancing through the code associated with the datetime field i guess when you don't explicitely set an output format it falls back to:

const defaultDateOutputFormat = 'Y-m-d'; // FieldtypeDatetime.module

On the db level FieldtypeDatetime is a MySQL datetime type.

getUnformatted seems to give you the wakeupvalue.

/** * Convert value from timestamp to Y-m-d H:i:s date string * */
public function ___sleepValue(Page $page, Field $field, $value) { return date('Y-m-d H:i:s', $this->_sanitizeValue($value)); }
/** * Convert value from Y-m-d H:i:s string to timestamp * */
public function ___wakeupValue(Page $page, Field $field, $value) { return strtotime($value); }
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...