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 3996366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:16:15+00:00 2026-05-20T07:16:15+00:00

I’m using Yii to construct a web application. One of my input forms has

  • 0

I’m using Yii to construct a web application. One of my input forms has a CJuiDatePicker for the date. I have two drop down lists, one for the hours, and one for the minutes.

My problem is in the data model, where I’m trying to convert the date, hour, and minute from the form into a MySQL datetime string. I have to produce a datetime string that looks like this – 2011-02-27 20:11:56, so Yii can convert the string into a MySQL datetime and insert the value into the row.

In the model, I have a rule that looks like this:

array('event_datetime_from', 'createDatetime', 
    'date'=>'event_date_from', 'hour'=>'event_hour_from',
    'minute'=>'event_minute_from'),

The createDateTime validator function looks like this:

public function createDatetime($attribute, $params) {
    if (!$this->hasErrors()) {      
        $date = $this->$params['date'];
        $hour = $this->$params['hour'];
        $minute = $this->$params['minute'];

        if (trim($date) === '') {
            $this->$attribute = null;
        } else {
            $parse = CDateTimeParser::parse(
                $date.' '.$hour.':'.$minute,
                'MM/dd/yyyy hh:mm');
            $this->$attribute = date('Y-m-d H:i:s', $parse);
        }
    }
}

Now, I’m not a PHP developer. However, it appears to me that $params['date'] is returning the string value event_date_from, rather than the value of event_date_from.

My PHP question is, how do I get the value of event_date_from inside the createDateTime validator function?

My apologies if I overlooked the answer somewhere in the Yii documentation. I couldn’t find many examples of validator functions. The Yii validator classes have a different parameter signature than validator functions.

Edited based on thaddeusmt’s answer:

I tried extending CActiveRecord and coded an afterValidate method, but I couldn’t find a place to define my working date, hour, and minute variables. I defined them in the extended method, and the afterValidate method couldn’t see them. I got a PHP undefined variable error in the afterValidate method.

In the controller, I coded the following function:

protected function createDateTime($dateString, $hour, $minute) {
    if (trim($dateString) == '') {
        return null;
    } else {
        $timeString = $dateString.' '.$hour.':'.$minute;
        return date('Y-m-d H:i:s', strtotime($timeString));
    }
}

It should be a cinch to call a function in PHP, right?

I tried both of these calls in the actionCreate() function:

$model->event_datetime_from = 
            createDateTime($_POST['event_date_from'],
            $_POST['event_hour_from'],
            $_POST['event_minute_from']
            );

and:

$model->event_datetime_from = 
            createDateTime($model->event_date_from,
            $model->event_hour_from,
            $model->event_minute_from
            );

My controller code dies with either of these calls, and I get a blank (no HTML) response page.

I thought what I wanted to do was pretty simple. I want to take a date, hour, and minute string, and convert the concatenation to a datetime string. What am I missing?

  • 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-20T07:16:16+00:00Added an answer on May 20, 2026 at 7:16 am

    What I do is, in the POST action in the Controller (where the POST vars are assigned), I convert the posted date and time values into a MySQL datetime with the date() and mktime() function, then validate/save. So, here is an example of the post action:

    public function actionUpdate() {
      $model=$this->loadModel();
      if(isset($_POST['Model'])) {
        $model->attributes = $_POST['Model']; // assign the rest of the POST vars here
        $model->event_datetime_from = date(
          'Y-m-d H:i:s', // convert the timestamp to the mySQL format
          mktime(        // create the timestamp from the posted date and time vars
            $_POST['my-hour-var'], // set the hour
            $_POST['my-minute-var'], // set the min
            $_POST['my-second-var'], // set the sec
            date("m"), // set the month
            date("d"), // set the day
            date("Y") // set the year
          )
        ); // create a MySQL Y-m-d H:i:s format date from the POST vars
        $model->save(); // this run the validation rules, naturally
      }
    }
    

    (This assumes a model called “Model”, POSTed hour, minute and second variables called my-hour-var, my-minute-var and my-second-var respectively, and that you are setting the DATE part to today.)

    And here is an example of validation rule in the Model model using the CTypeValidator:

    public function rules() {
      return array(
        array('event_datetime_from', 'type', 'type'=>'datetime', 'datetimeFormat'=>'yyyy-MM-dd hh:mm:ss', 'message' => '{attribute} is not a date and time!'),
    }
    

    I hope this helps!

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.