Peter Falkenberg Brown Posted September 24, 2021 Posted September 24, 2021 Hi All, Maybe because my brain is fried tonight, but I'm totally stuck on extracting the 4 digit year from a datetime field inside the ready.php file, on saving a page. Here's my code: $pages->addHook('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if ( $page->template == 'book_sales' ) { if ( $page->sold == 1 ) { $page->profit = ($page->sale_price - $page->buy_cost) - $page->transaction_fee; $page->sale_year = $datetime->date($format='Y', $page->getUnformatted(sale_date)); } } }); The line that tries to get the sale_year, i.e.$page->sale_year = $datetime->date($format='Y', $page->getUnformatted(sale_date)); breaks with a bunch of errors. Anyone have the right syntax? Thanks! Peter
AndZyk Posted September 24, 2021 Posted September 24, 2021 Hi @Peter Falkenberg Brown, I think it should be: $page->getUnformatted('sale_date') Personally I have never used the $datetime->date()-function. I am using the plain PHP date()-function. Regards, Andreas
Peter Falkenberg Brown Posted September 24, 2021 Author Posted September 24, 2021 Thanks, @AndZyk, But adding the single quote marks didn't work. I still get this error message: Fatal Error: Uncaught Error: Call to a member function date() on null in site/ready.php:12 #0 wire/core/WireHooks.php (1050): ProcessWire->{closure}(Object(HookEvent)) #1 wire/core/Wire.php (485): WireHooks->runHooks(Object(Pages), 'saveReady', Array) #2 wire/core/PagesEditor.php (491): Wire->__call('saveReady', Array) #3 wire/core/PagesEditor.php (455): PagesEditor->savePageQuery(Object(Page), Array) #4 wire/core/Pages.php (799): PagesEditor->save(Object(Page), Array) #5 wire/core/Wire.php (420): Pages->___save(Object(Page), Array) #6 wire/core/WireHooks.php (951): Wire->_callMethod('___save', Array) #7 wire/core/Wire.php (485): WireHooks (line 12 of site/ready.php) Peter
BitPoet Posted September 24, 2021 Posted September 24, 2021 3 hours ago, Peter Falkenberg Brown said: The line that tries to get the sale_year, i.e.$page->sale_year = $datetime->date($format='Y', $page->getUnformatted(sale_date)); breaks with a bunch of errors. Anyone have the right syntax? You're inside a function, so there's no global $datetime object. wire('datetime') should do the trick. 2
Peter Falkenberg Brown Posted September 24, 2021 Author Posted September 24, 2021 Thanks, @BitPoet! That worked perfectly. It's always the little things. :-) Peter
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