Setting default time for timedate field

edited July 2014 in Modules
I have a datetime field 'post_date' for a model. It uses the date/time picker, but when left blank, I want it to default to the current timestamp. The date works, but the time is 00:00:00. In on_before_clean and on_before_post, I have this:

if (empty($values['post_date'])) { $values['post_date'] = datetime_now(); }

Comments

  • edited 7:54AM
    What version of FUEL are you running?
  • edited 7:54AM
    1.0.2
  • edited 7:54AM
    I would try updating to 1.1 to see if that fixes your problem. If you are using GIT, you could do a GIT pull.
  • edited 7:54AM
    Is there a guide for updating Fuel if you don't use GIT?
  • edited 7:54AM
    Try just replacing the fuel/modules/fuel folder.
  • edited 7:54AM
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: CI_Exceptions::$load

    Filename: _blocks/header.php

    Line Number: 23

    Fatal error: Call to a member function view() on a non-object in C:\xampp\htdocs\project\fuel\application\views\_blocks\header.php on line 23
  • edited 7:54AM
    That happens when I access http://localhost/project/fuel now.
  • edited 7:54AM
    In _blocks/header, replace anywhere that it says "$this" with "$CI" and see if that fixes the issue.
  • edited July 2014
    Getting a 404 Page Not Found error when trying to access the Fuel directory. Going to do a fresh install.
  • edited July 2014
    Ok, after all that, I'm still getting 00:00:00.

    function on_before_clean($values) { if (empty($values['post_date'])) { $values['post_date'] = datetime_now(); } return $values; } function on_before_post($values) { if (empty($_POST['post_date'])) { $_POST['post_date'] = datetime_now(); } return $values; }
  • edited 7:54AM
    I think I see what may be going on. The date field field is actually combined together form the $_POST['post_date'], $_POST['post_date_hour'], $_POST['post_date_min'] and $_POST['post_date_ampm']. The clean method combines those together to create the $values['post_date'] so the on_before_clean method won't work nor the on_before_post which happens before the other saving hooks. I would try the "on_before_save" method instead which happens right before saving and after cleaning the data.

    Sorry to have you download the latest. There were some issues a few releases back that I thought may have been the issue.
  • edited July 2014
    Edit: Wrong thread.
  • edited August 2014
    Using on_before_save had the same results, however, this works.

    function on_before_clean($values) { if (empty($values['post_date'])) { $values['post_date'] = datetime_now(); } elseif ($values['post_date'] == date('Y-m-d H:i:s', strtotime('today midnight')) && empty($values['post_date_hour'])) { $values['post_date'] = datetime_now(); } return $values; }
  • edited 7:54AM
    Hmm... Not sure what may be going on. I just tried it on a model and both on_before_save and on_before_clean worked. What format are you using for the date in the datetime picker (default is mm/dd/yyyy)?
  • edited 7:54AM
    For a different module, I ran into the same issue of post_date defaulting to 12pm. But I also have an expiry_date field, and it works correctly. So they're both set to datetime_now(); but only expiry_date saves as the current time. Any ideas?
  • edited 7:54AM
    Could you provide the hook code you are using?
  • edited August 2014
    After messing around with it more, I realized it only happens because it's also set in the on_before_post hook (and expiry_date is not). I needed to have it set in on_before_post because so the file name doesn't become 1969-12-31:
    $_POST['new_file_name'] = url_title(date('Y-m-d', strtotime($_POST['post_date'])).'-'.captionShort($_POST['content']), 'dash', TRUE);

    Fixed the issue doing this:
    function on_before_post($values) { $post_date = (empty($_POST['post_date'])) ? date('Y-m-d') : $_POST['post_date']; $_POST['new_file_name'] = url_title(date('Y-m-d', strtotime($post_date)).'-'.captionShort($_POST['content']), 'dash', TRUE); return $values; }

    And setting $values['post_date'] = datetime_now(); in on_before_clean or on_before_save works fine. Thanks!
Sign In or Register to comment.