Jump to content

[SOLVED] get "YEAR" from datetime field with API


Peter Falkenberg Brown
 Share

Recommended Posts

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
 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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