Advertisement:

Author Topic: Convert in seconds  (Read 37372 times)

fog

  • Hero Member
  • *****
  • Posts: 1062
Convert in seconds
« on: May 04, 2016, 03:14:03 pm »
Hi, I need convert these dates only in seconds, how I do?  ???

Unless somebody can tell me how I can add hours/minutes and seconds on a custom field date with date picker. This parameter is 00:00:00 always, why not adding by default the current hour/minute/secounds?

Code: [Select]
<?php
"dt_pub_date" "dt_mod_date" "dt_expiration"

How i can get current time, only with content: hours/minutes/seconds of  "dt_pub_date" and "dt_mod_date"?

Add this value with some variable on a complete date with 00:00:00, can change the value only this parameter?

I'm a bit lost on this.

Regards
« Last Edit: May 04, 2016, 03:42:56 pm by fog »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Convert in seconds
« Reply #1 on: May 04, 2016, 06:00:14 pm »
Hi fog,

Code: [Select]
osc_format_date(osc_item_pub_date()) . preg_replace('/^.*( .*)$/', '$1', osc_item_pub_date())
Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #2 on: May 04, 2016, 08:10:37 pm »
Hi teseo,

If I use echo on that line is not show me in seconds (timestamp). Is possible print on seconds these helpers?

Maybe exists some solution for add hours/minutes/seconds on custom field with date picker, but I don't idea how it can be possible.

My problem is that, the custom field with date picker not add any time(hours/minutes/seconds). The problem I need these parameters to reverse a countdown time  :-\

Do you suggest me any idea for I obtain current date(hours/minutes/sec.) on date custom field with date picker?

EDIT:

Allow me correct me: the date custom field with date picker, after convert the seconds on http://www.epochconverter.com/

The dates appear with same hour:

2016-05-02 23:00:00
2016-04-21 23:00:00
2016-04-30 23:00:00
...

I really need obtain the same hour/minute/second as "osc_item_pub_date" and "osc_item_mod_date". Exists some solution for I can obtain this?

Thanks
Regards
« Last Edit: May 04, 2016, 08:33:01 pm by fog »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Convert in seconds
« Reply #3 on: May 04, 2016, 09:22:17 pm »
If I use echo on that line is not show me in seconds (timestamp). Is possible print on seconds these helpers?

If the date came from a datepicker, hour will be 00:00:00. I've been working with datepicker lately and I think this is a solution:

Code: [Select]
var prevHandler = $(".cf_date").datepicker('option','onSelect');

$(".cf_date").datepicker('option', 'onSelect', function() {
    prevHandler.call(this);
                     
    var now = Math.floor(Date.now() / 1000);
                     
    var today = new Date();
    var startOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate());
    var timestamp = startOfDay / 1000;

    timestamp = parseInt($(this).prev('input').val()) + (now-timestamp);
    $(this).prev('input').val(timestamp);                 
});

If the date selected is May 18 and current time is 18:16:10 hours, the final stored value (a Unix timestamp) will produce May 18 18:16:10

I hope this is what you're looking for, because took me a little while... :D

Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #4 on: May 04, 2016, 10:30:01 pm »
hi teseo, thanks for your time on this. I really admire what you do on this forum. Of course, time is precious.

But I cannot follow you, is javascript, but how I can use it?

I really mean, what I need to do in concrete?

This is the code for custom field for a function you post in times and I use it with a variable $epoch:
Code: [Select]
<?php $epoch cust_get_custom_field_item_value($cf_name) ; ?>
and this is the code for showing the dates:
Code: [Select]
<?php $epoch cust_get_custom_field_item_value($cf_name) + 86400$dt = new DateTime("@$epoch"); ?>
<?php $day 86400$interval $epoch $day?>
<?php if(time() > $interval && time() < $epoch ) {?><strong>Expira hoje!</strong>
<?php } elseif(time() < $interval) { ?>Expira a <?php  echo $dt->format('j F Y g:i:s'); ?>
<?php } elseif(time() > $interval && time() > $epoch){ ?>Expirou a <?php  echo $dt->format('j F Y g:i:s'); ?>
<?php ?>
On a specific plugin I have this function:
Code: [Select]
<?php
function updateItemExpired($item) {
$day 86400;
$epoch cust_get_custom_field_item_value($cf_name) + 86400/* return the fix date expire value chosen */
$dt = new DateTime("@$epoch") ;
$conn getConnection();
$conn->osc_dbExec("UPDATE %st_item SET dt_expiration = '".$dt->format('Y-m-d H:i:s')."' WHERE pk_i_id = '%d'"DB_TABLE_PREFIX$item['pk_i_id']);
}
osc_add_hook('posted_item''updateItemExpired');
osc_add_hook('edited_item''updateItemExpired');

On your javascript code I change the name .cf_date to cf_name (don't know if is needed).

Where I need to put the javascript code, creating a new function on functions.php and add hook to post/edit, or other situation?

Sorry, I really don't know.
Thanks again

Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #5 on: May 04, 2016, 10:48:21 pm »
Well, I tried using your code directly on my plugin inside the function, but no luck after edit a item on date custom field. She return the same hour again: Expira a 9 May 2016 11:00:00

I tried on item_form.php file of plugin on bottom... but no luck again.

Maybe tomorrow you can explain me something I missed.

Thank you

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Convert in seconds
« Reply #6 on: May 04, 2016, 10:49:48 pm »
Yes, it's Javascript code that you need to add to the page:

Code: [Select]
<?php
function cust_datepicker_add_time() { ?>

    <script type="text/javascript">
        var prevHandler = $(".hasDatepicker").datepicker('option','onSelect');

        $(".hasDatepicker").datepicker('option', 'onSelect', function() {
            prevHandler.call(this);

            var now = Math.floor(Date.now() / 1000);

            var today = new Date();
            var startOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate());
            var timestamp = startOfDay / 1000;

            timestamp = parseInt($(this).prev('input').val()) + (now-timestamp);
            $(this).prev('input').val(timestamp);
        });
    </script>
<?php }

osc_add_hook('item_form''cust_datepicker_add_time'10);
osc_add_hook('item_edit''cust_datepicker_add_time'10); 
?>


It'll intercept Date selection on any datepicker input and add current day time to the date selected by the user. "cf_date" class as element identifier was correct to manage DATE custom fields, but now I've replaced it with class "hasDatepicker", common to DATE and DATEINTERVAL types.

Regards
« Last Edit: May 04, 2016, 11:29:38 pm by teseo »

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #7 on: May 04, 2016, 11:01:20 pm »
But not working. I added on functions file. After I publish a new item or edit one, the date of expire have the same hour: Expira a 18 May 2016 11:00:00

I'm on localhost, I check PhpMyAdmin and the date with hour is really that.(23:00:00).

 :-\

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Convert in seconds
« Reply #8 on: May 04, 2016, 11:33:41 pm »
Check my corrected post above. Initially I gave you something meant for search filters, I was working on that when I needed to study how to dynamically alter Datepicker settings (tough matter, seriously :D ).

See if now it's working for you, regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #9 on: May 05, 2016, 04:56:05 am »
Hi teseo, sorry my long delay in response. I was out before with friends, and now I'm home again.

Your javascript function works fine to add the current hours. Really nice work!

I found a little problem with your function activated: when I edit a item, the stored value of date picker not appears on the field. Some side effects with this new function?

This problem appears to you on your side?

Or maybe it can be caused with other javascript on my codes with this new function. Tomorrow I go testing removing some javascript code I have for see what happens.

Let me know when you can tell me with your experience, if this problem can appears to you.

Thank you very much !

MY GOD.... :o :o :o (Read 15676 times) in a unique day?!!! This is a record, mistake or what?

Anyway, I think you saved a LOT of people today, to custom field date picker with current hour ;D  8)

Best Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Convert in seconds
« Reply #10 on: May 05, 2016, 08:38:52 pm »
found a little problem with your function activated: when I edit a item, the stored value of date picker not appears on the field. Some side effects with this new function?

I guess so. Yes, the problem exists, I'll get back to you tomorrow.

Quote
MY GOD.... :o :o :o (Read 15676 times) in a unique day?!!! This is a record, mistake or what?

Don't know why, but I've seen that before. A bug, an attack? ???

Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Convert in seconds
« Reply #11 on: May 06, 2016, 06:08:51 am »
Hi teseo, glad to know you go look on it.

Humm,... It's very weird so many quick views, I think is not normal for a bug. Maybe some malicious.

Best regards friend  :)