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:
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!
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:
I’ve never had luck doing date comparisons without converting them to UNIX first.