WordPress.org

Forums

[Plugin: Custom Field Template] store Date ind DB as timestamp (8 posts)

  1. skarck
    Member
    Posted 5 years ago #

    Hi!

    First of all I would like to say that this is an awesome plugin.
    When I discovered the date-Picker I wondered if it's possible to store the date as a kind of timestamp in the DB while still viewing the chosen date (in the input field) in the defined format.

    That would help sorting posts by dates and still using unusual date-formats..

    thanks in advance!

    http://wordpress.org/extend/plugins/custom-field-template/

  2. Hiroaki Miyashita
    Member
    Posted 5 years ago #

    Hi skarck,
    Please try to use code and editCode attributes. This is an example:

    TEMPLATE CONTENT #0

    [date]
    type = text
    date = true
    code = 0
    editCode = 1

    CODE #0

    $value = get_post_meta( $_REQUEST['post'], $title, true);
    if ( $value ) $enforced_value = date('d/m/Y', $value);

    CODE #1

    $value = date('U', mktime(0, 0, 0, substr($value, 3, 2), substr($value, 0, 2), substr($value, 6, 4)));

    The value will be saved as timestamp. Thanks.

  3. skarck
    Member
    Posted 5 years ago #

    thanks,

    one more reason to love this plugin!

  4. lasson
    Member
    Posted 5 years ago #

    Trying to do this also… but it sticks to the value calculated in CODE#1 which means that next time it recalculates to the start date (1970).

    the code I try

    TEMPLATE CONTENT #0

    [ev_date]
    type = text
    date = true
    startDate = '1970/01/01'
    dateFormat = yyyy/mm/dd
    code = 0
    editCode = 1

    CODE #0

    $value = get_post_meta( $_REQUEST['post'], $title, true);
    if($value) {$enforced_value = date('Y/m/d', $value);}

    CODE #0 (also tried)

    $value = get_post_meta( $_REQUEST['post'], $title, true);
    if ($value) $enforced_value = date('Y/m/d', $value);

    CODE #1

    $a = explode("/",$value);
    $value = date('l, j F Y', mktime(0, 0, 0, $a[1], $a[2], $a[0]));
  5. Omission
    Member
    Posted 4 years ago #

    lasson, would the simplest thing to do to be just remove the startDate variable? Thats what I did?

    Congrats Hiroak, on the great plugin, something like this should be in the core, and this function here to create the timestamp is ace, the only thing that would make it perfect is that the Date is now shown as a time stamp on in the admin ( Obviously! ), however it would be great if we could then force this timestamp into date() function in the admin to make it readable?

    Would be cool to have a timeStamp: variable so that we can order by the dates accurately in the wp_query but then have human readable dates in the admin also.

  6. exoticcorpse
    Member
    Posted 4 years ago #

    Would you have any suggestions on how to add a Time to the timestamp?

  7. acebone
    Member
    Posted 4 years ago #

    if you get a warning using the code-examples - then use this for code #0

    $value = get_post_meta( $_REQUEST['post'], $title, true);
    if ( $value ) $enforced_value = date('d/m/Y', (int)$value);

    It converts $value from string to integer before feeding it to the PHP date function - which means you won't get a warning.

    Actually use this even though you don't get a warning - you system-conf might change one day, and all of a sudden the warning will be there

  8. schmaart
    Member
    Posted 4 years ago #

    @omisson -- did you get this working with query_posts(orderby= ...

Topic Closed

This topic has been closed to new replies.

About this Topic