Jump to content

FieldtypeDatetime output formatting


MuchDev
 Share

Recommended Posts

So I can't seem to find the full documentation on the datetime field and was wondering how I would go about formatting the output via the api. If i were to

echo $page->date 

I get the date perfectly. What I would like to do is in some instances just grab a month day or year so that I could run loops that print different info on the page in an archive. I have found some complicated code that I didn't entirely understand and the documentation from the field didn't seem to have what I needed. 

Is there some way I could just do something like 

echo $page->date('Month-Year');

//or better yet

$month = $page->date('month');
$year = $page->date('year');
  • Like 1
Link to comment
Share on other sites

It could be that you have some output formatting applied in the field settings. In that case you should first get the Unix timestamp, like this:

$month = date("F", $page->getUnformatted("date")); // where date is your fieldname
  • Like 4
Link to comment
Share on other sites

You guys totally nailed it, I had formatting and didnt think about the whole unix timestamping! I now understand a lot more about how that datetime field works. So many code options, now I can use formatting and still use the field to grab specific parts of the date. 

**edit ** for some reason what I said came out snarky :) 

This might be really helpful if it made it's way to the cheat sheet is what I meant. 

Link to comment
Share on other sites

This might be really helpful if it made it's way to the cheat sheet is what I meant. 

Well getUnformatted is in the cheatsheet. Are you referring to the date function not being in there? Only reason is that it's a pure PHP function, rather than something that is part of the PW API so it doesn't really belong. I feel like it might belong in here (http://processwire.com/api/fieldtypes/) once Ryan gets around to adding info about the datetime fieldtype.

  • Like 2
Link to comment
Share on other sites

I do agree that pure php functions don't belong, that is what their documentation is for. More I was just casually musing on processwire's implementation of the date time field as the only real information is the description on the module itself refering the user to php's documentation without any samples of the syntax.  I suppose though the function is exactly the same it makes sense. Now I understand :) Anyway Im just going to unformat the fields and talk to them with php as I will most likely be needing the ability to display the date in many different formats. You guys are total life savers.

Link to comment
Share on other sites

@adrian

getUnformatted is definitely the PW way :), but i'm not entirely sure that it saves a conversion step. Seeing as a datetime field is a Y-m-d H:i:s field on the database side of things a strtotime is always necessary. I've always thought that getUnformatted on a datetime field gives you the wakeupValue:

// in FieldtypeDatetime.module

	/**
* Convert value from Y-m-d H:i:s string to timestamp
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
if(empty($value)) return '';
$value = strtotime($value);
if($value === false) $value = '';
return $value;
}

I'm guessing this wakeupValue is always present, if thats the case it does indeed seem to save a conversion

  • Like 2
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...