Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We are struggling to find a way to calculate a time difference between two dates saved as custom fields on WP. We are actually using two different plugins:

a) Easy Content Types

b) Advanced post list

With the first we have created a series of custom fields containing start (ecpt_data-consegna) and end (ecpt_data-dismissione) dates, and we would like to find a way to show the time difference between those dates - shown as XX years, XX months, XX days - in a cell of a table generated through the Advanced Post List plugin. Please note that shall the eding date be missing, the difference should be calculated between the starting date and the current date (now).

After much chatting with both plugins' developers, we came to a standstill. Currently we have developed a custom PHP function to handle the date calculations as follows:

function calculateDiff() {
$start_date = new DateTime(get_post_meta( get_the_ID(), 'ecpt_data-consegna', true ));
$since_start = $start_date->diff(new DateTime(get_post_meta( get_the_ID(), 'ecpt_data-dismissione', true )));

if($since_start->days == 0) {
        $since_start = $start_date->diff(new DateTime());
}

return $since_start->y . ' years, ' . $since_start->m . ' months, ' . $since_start->d . ' days'; }

But when we put this APL shortcode inside one of the APL-generated cells:

<td>[php_function name="calculateDiff"]</td>

The result is a broken web page, cut off before the generation of the table itself. And when we try to use this code instead:

<td><?php echo calculateDiff();?></td>

The result is a blank cell.

We have also tried to call the function directly (outside APL) and the result is always a blank. So there must be something wrong somewhere...

Any help would be greatly, greatly appreciated!

share|improve this question
    
Any debug information? There should be an error message somewhere. –  toscho Oct 30 '12 at 9:24

1 Answer 1

I don't know how much this will help, but when I've done date/time comparisons, I've always had to convert them to UNIX timestamps first (converts the date/time to a numeral, which you can compare as less than/greater than/equal to the next number), and then after the difference is sorted, convert it back with date().

A simple example (not of your code, but of a quickie I did for a client a couple of years ago) was to do this:

$today = date('d-m-Y');                       // today's date
$today = strtotime($today);                    // UNIX conversions  

I've never had luck doing date comparisons without converting them to UNIX first.

share|improve this answer
    
Thanks Shelly for your suggestion, but in the opening post I didn't mention that the Easy Content Types plugin does already convert all the dates to UNIX timestamps. So that step has been done already. –  Diego Meozzi Oct 30 '12 at 13:42
    
Ah, well that makes a difference then ;) I'm not familiar with the plugin though - have you tried contacting the plugin authors? Maybe it's a bug they're aware of? –  Shelly Oct 30 '12 at 17:08
    
Well, I have already contacted the authors of both plugin, but as I'd like to use a custom function, that is clearly outside their support duties... –  Diego Meozzi Oct 31 '12 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.