MuchDev Posted August 25, 2014 Share Posted August 25, 2014 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'); 1 Link to comment Share on other sites More sharing options...
adrian Posted August 25, 2014 Share Posted August 25, 2014 There is no need for special API formatting - just make use of the PHP date function: http://php.net/manual/en/function.date.php $month = date("F", $page->date); 2 Link to comment Share on other sites More sharing options...
MuchDev Posted August 25, 2014 Author Share Posted August 25, 2014 Thanks as always! Sorry for cloggin up the airwaves. Im not getting anything out of that code, but I'll keep tinkering. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted August 25, 2014 Share Posted August 25, 2014 The php date function needs a unix timestamp. $month = date("F", 1408946806); // here's the $page->date replaced with the value. echo $month; // outputs: Augustus 3 Link to comment Share on other sites More sharing options...
adrian Posted August 25, 2014 Share Posted August 25, 2014 This should do it if your date is already formatted: $month = date("F", strtotime($page->date)); //strtotime converts a formatted date string back to a unix timestamp 5 Link to comment Share on other sites More sharing options...
SiNNuT Posted August 25, 2014 Share Posted August 25, 2014 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 4 Link to comment Share on other sites More sharing options...
adrian Posted August 25, 2014 Share Posted August 25, 2014 Nice one Sinnut - for some reason I always forget about "getUnformatted" - that is definitely the right PW way to do it and I guess one less conversion step along the way! 3 Link to comment Share on other sites More sharing options...
MuchDev Posted August 25, 2014 Author Share Posted August 25, 2014 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 More sharing options...
adrian Posted August 25, 2014 Share Posted August 25, 2014 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. 2 Link to comment Share on other sites More sharing options...
MuchDev Posted August 25, 2014 Author Share Posted August 25, 2014 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 More sharing options...
SiNNuT Posted August 25, 2014 Share Posted August 25, 2014 @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 2 Link to comment Share on other sites More sharing options...
MuchDev Posted August 26, 2014 Author Share Posted August 26, 2014 Well I got some great results due to your awesome assistance. Now I have a monthly, and yearly display that runs off one single date field. Thanks again Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now