Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3226124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:25:09+00:00 2026-05-17T16:25:09+00:00

I have a datetime value in my Event model. Not being a big fan

  • 0

I have a datetime value in my Event model. Not being a big fan of the default aesthetics of Cake’s automagical input, I’m going to create my own. My understanding, though only from reading, is that Cake provides some intelligence around concatenating datetime values on save. Specifically, it will detect month, day, year, hour, minute and meridian subparts of a datetime value and automatically combine them on save.

What I’m more interested in, but can find no evidence of, is a simpler breakdown. I’d like to use a date picker for the date part and the default time input for, you guessed it, the time. Can Cake do this? I have tried it and it’s not working, but I’m not sure whether that’s a result of me doing the wrong thing or the right thing not being available.

I can certainly do this myself without much effort, but some effort is greater than no effort if Cake already has this built-in and I’m just not unlocking it properly.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:25:09+00:00Added an answer on May 17, 2026 at 4:25 pm

    A trip through the source code, specifically Model::deconstruct() told me that Cake’s native intelligence doesn’t run this deep. It expects the full component granularity provided by its own datetime input (i.e. separate datetime components for month, day, year, hour, minute and meridian). I wanted a better user experience, so I wanted to use a date picker that stores the entire date as a single component. To do that I had to get creative.

    What I chose to do was to override the Model::deconstruct() method to break down the date into its components. Here’s some code to help visualize the solution I expect to deploy:

    # 
    # In my form (views/events/_form.ctp)
    # When rendered, the input names resolve to data[Event][start_time][date], 
    # data[Event][start_time][hour], data[Event][start_time][minute], 
    # data[Event][start_time][meridian]
    # 
    <div class="input datetime required">
      <?php echo $this->Form->input( 'Event.start_time.date', array( 'type' => 'text', 'label' => 'Start Time', 'div' => false, 'class' => 'date' ) ) ?>
      <?php echo $this->Form->input( 'Event.start_time', array( 'type' => 'time', 'label' => false, 'div' => false, 'class' => 'time', 'empty' => true ) ) ?>
    </div>
    
    # 
    # In my model (/models/event.php)
    # 
    function deconstruct( $field, $data ) {
      $type = $this->getColumnType( $field );
    
      if( in_array( $type, array( 'datetime', 'timestamp' ) ) ) {
        if( isset( $data['date'] ) && !empty( $data['date'] ) ) {
          $date = date( 'U', strtotime( $data['date'] ) );
    
          if( $date ) {
            $data['month'] = date( 'm', $date );
            $data['day']   = date( 'd', $date );
            $data['year']  = date( 'Y', $date );
          }
        }
      }
    
      # Now the date is broken down into components that the
      # well-vetted parent method expects. Let it do the heavy 
      # lifting.
      return parent::deconstruct( $field, $data );
    }
    

    For clarity, I’ve excluded the javascript code that creates the picker itself. It’s a simply jQuery UI behavior applied to the data[Event][start_time][date] field and isn’t germane to the solution. Suffice to say, when the date textbox receives the focus a very pretty calendar appears.

    In my case, I also have several models that have datetime properties so I ended up moving the deconstruct() override out to a my AppModel so I can use it throughout. DRY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table and a column of type datetime. It's default value
I have created mysql to delete table rows, which have lower DATETIME value than
I have a UTC DateTime value coming from a database record. I also have
I'm trying to count the number of records that have a null DateTime value.
I have a Oracle procedure to which I have to pass a datetime value
I have the following line of code: group p by new DateTime((p.DateTime.Value.Ticks / interval.Ticks)
I have the following: PMRCutoffDate.Value = ReportCalc.Cutoff_Date.ToString(MM/dd/yyyy); PMRDate.Value = DateTime.Now.ToString(MM/dd/yyyy); i want dates to
I have startdate in QueryString with value: 3/1/2012 DateTime.Parse(Request.QueryString[startdate]).Month return month number: 1 but
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a Joda-DateTime field in my model and want to display it formatted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.