Advertisement:

Author Topic: <SOLVED> function to calculate date difference from DATE-INTERVAL Custom Field  (Read 2555 times)

ronatforums.osclass

  • Newbie
  • *
  • Posts: 6
Hi all -

can someone point me in the right direction on how to achieve the following:

use the custom field DATE-INTERVAL to
a) calculate the difference in days between the 2 dates and display it along the items information page
b) retrieve only the first date from the DATE-INTERVAL field and display it

the final result should look like this:
User input: from August 20, 2013 to August 25, 2013

shown information on item page: 6 days - start on August 20, 2013

any help or coding samples would be much appreciated.

cheers
« Last Edit: August 21, 2013, 01:22:36 am by ronatforums.osclass »

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Hi all -

can someone point me in the right direction on how to achieve the following:

use the custom field DATE-INTERVAL to
a) calculate the difference in days between the 2 dates and display it along the items information page
b) retrieve only the first date from the DATE-INTERVAL field and display it

the final result should look like this:
User input: from August 20, 2013 to August 25, 2013

shown information on item page: 6 days - start on August 20, 2013

any help or coding samples would be much appreciated.

cheers

Maybe something like this:

Code: [Select]
    $today    =  (int) (strtotime(date('Y-m-d H:i:s'))/86400);
    $pubdate = (int) (strtotime($item['dt_pub_date'])/86400);
    $interval = $today - $pubdate;
    echo 'Interval is '.$interval.' days';

ronatforums.osclass

  • Newbie
  • *
  • Posts: 6
byteGator -

thanks for looking into it.

i used your example but still struggle with some issues.

here is how i use it:

in my templates function.php file i use this.

Code: [Select]

function  osc_date_difference($dateinterval) {

if(!function_exists('get_string')){
function get_string($string, $start, $end) {
       $start = preg_quote($start, '|');
       $end = preg_quote($end, '|');
       $matches = preg_match_all('|'.$start.'([^<]*)'.$end.'|i', $string, $output);
       return $matches > 0
           ? $output[1]
           : array();
}       

$string = $dateinterval;
$startdate = get_string($string, 'From', 'to');
$enddate = get_string($string, 'to', '');   

$start    =  (int) (strtotime($startdate[0])/86400);
        $end = (int) (strtotime($enddate[0])/86400);
        $interval = $end - $start;
        echo 'Interval is '.$interval.' days';
}
        }


in my item.php file i use this:

Code: [Select]

<?php

$trip_duration 
'From August 25, 2013 to August 28, 2013';

echo 
osc_date_difference($trip_duration);

?>



in my item.php file i use the function osc_date_difference($trip_duration); several times in order to display (the interval in days)

it works for the first occurrence, but using the same function in the same file a second or third time, just displays nothing.

e.g.

<h1> <?php echo osc_date_difference($trip_duration); ?> </h1> //displays 3 days, given the string 'From August 25, 2013 to August 28, 2013'
<h2> <?php echo osc_date_difference($trip_duration); ?> </h2> //displays nothing

<h1> and <h2> are used in the same file on the same page.

am sure i just messed up with the functions, but cant figure out what should be the right way.

in simple. i have a string (From August 25, 2013 to August 28, 2013) which i need to extract the start and the end date, calculate the difference in days, and display it several times on my page.

i also thought maybe to get the date values direct from the datebase instate of extracting it out from the string, but i dont know how to get the values from the database. thats why i use the approach with extracting from the string.

hope i made it not to difficult to explain.

any idea or advice on how to achieve this?

thanks for helping

cheers

ronatforums.osclass

  • Newbie
  • *
  • Posts: 6
i solved it myself.

in case somebody else needs it, here is what i came up with:

Code: [Select]
function  osc_date_difference($dateinterval) {

if(!function_exists('get_string')){
function get_string($string, $start, $end) {
$start = preg_quote($start, '|');
$end = preg_quote($end, '|');
$matches = preg_match_all('|'.$start.'([^<]*)'.$end.'|i', $string, $output);
return $matches > 0
? $output[1]
: array();
}}

$string = $dateinterval;
$startdate = get_string($string, 'From', 'to');
$enddate = get_string($string, 'to', ''); 

$start    =  (int) (strtotime($startdate[0])/86400);
$end = (int) (strtotime($enddate[0])/86400);
$interval = $end - $start;
if ($interval < "1")
  {
  //do nothing
  }
else
  {
    $interval = $interval+1;
    echo $interval.' Days ';
  }}

fog

  • Hero Member
  • *****
  • Posts: 1062
Hi, thanks you share this

Tell me, this code function is placed in functions.php, right?

And the item.php i put this?

Code: [Select]

<?php echo osc_date_difference($trip_duration); ?>


I try this, but nothing happens

Thanks for your help reply

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Hi Ronat

Inside function, better use 'return' than 'echo'

regards

ronatforums.osclass

  • Newbie
  • *
  • Posts: 6
byteGator -

thanks for this tip!

ronatforums.osclass

  • Newbie
  • *
  • Posts: 6
Fog -

yes, you are right.

the function goes into the templates function.php file.

the call in your item page is also correct.

the reason you dont see anything is because the variable $trip_duration is my own, and not from osclass core code.

if you write in your template:

Code: [Select]
<?php

$trip_duration 
'From August 25, 2013 to August 28, 2013'// this is the string the function use to convert text into date

echo osc_date_difference($trip_duration);//this sends the content of variable $trip_duration (which is the above string) to function.php file and returns the functions calculation, in my case the difference in days From August 25, 2013 to August 28, 2013

?>



now its up to you to the "fill" the variable $trip_duration with some dynamic generated data.

in my case, i use the DATE-INTERVAL field from osclass custom fields.

here is how i get to my custom fields: [HOW TO DISPLAY ONLY ONE CUSTOM FIELD IN TEMPLATE ITEM.PHP FILE]

1. the below code goes into your item.php file
Code: [Select]

<?php 
//INIT CUSTOM VARS
unset($trip_duration);

if( osc_count_item_meta() > ) {
while (osc_has_item_meta() ) {
if (osc_item_meta_id()==1) $trip_duration  osc_item_meta_value() ;

}
?>



NOTE: (osc_item_meta_id()==1) the number 1 stands for the custom field id number. you can easily find the correct id number by looking into your database under oc_t_meta_fields (the value for pk_i_id) or by just playing around with 1 2 3 4 and so on till the right custom field appears.

2. you can then add the function call
Code: [Select]

<?php 
//INIT CUSTOM VARS
unset($trip_duration);

if( osc_count_item_meta() > ) {
while (osc_has_item_meta() ) {
if (osc_item_meta_id()==1) $trip_duration  osc_item_meta_value() ;

}
?>


<?php echo osc_date_difference($trip_duration); ?>//here will appear the difference in days when used with the id number of DATE-INTERVAL custom field


TIP: to just print any custom field in your template, just echo the variable you "connected" to your custom field like so:

the below code goes into your item.php file
Code: [Select]

<?php 
//INIT CUSTOM VARS
unset($your_var1);
                                unset(
$your_var2);

if( osc_count_item_meta() > ) {
while (osc_has_item_meta() ) {
if (osc_item_meta_id()==1) $your_var1  osc_item_meta_value() ;
if (osc_item_meta_id()==2) $your_var2  osc_item_meta_value() ;
}
?>


<h2>this is <?php echo $your_var1?> and this is <?php echo $your_var2?></h2>


3. in your templates function.php file add this code for the date calculation. remember to make sure the variable $trip_duration gets its "content" from DATA-INTERVAL custom field specified trough the correct id number in (osc_item_meta_id()==1)
Code: [Select]

function  osc_date_difference($dateinterval) {

if(!function_exists('get_string')){
function get_string($string, $start, $end) {
$start = preg_quote($start, '|');
$end = preg_quote($end, '|');
$matches = preg_match_all('|'.$start.'([^<]*)'.$end.'|i', $string, $output);
return $matches > 0
? $output[1]
: array();
}}

$string = $dateinterval;
$startdate = get_string($string, 'From', 'to');
$enddate = get_string($string, 'to', ''); 

$start    =  (int) (strtotime($startdate[0])/86400);
$end = (int) (strtotime($enddate[0])/86400);
$interval = $end - $start;
if ($interval < "1")
  {
  //do nothing
  }
else
  {
    $interval = $interval+1;
    return $interval.' Days ';
  }}


hope this helps, as most of this information i got from this forum and "pick-pieced" it together.

cheers