我在這裏有一個表單(https://nursinggroup.com/pharmacist-profile/),其中用戶將以「mm/yy」的形式爲機器人「從」輸入框輸入日期, 「到」輸入框。在用戶完成並添加所有日期後,他們提交表單,並使用Gravity Forms PDF Extended(mPDF)創建PDF。我需要做的是,在創建PDF期間,我需要它來計算每個項目的值,並將每個項目的總年數分配到PDF上。PHP |重力形式| mPDF - 從「從 - 到」日期計算多少年
因此,舉例來說,如果我在網上表單上輸入以下內容輸入:
Ambulatory Care "From: 02/88" "To: 02/98"
Home Healthcare "From: 07/02" "To: 07/05"
Oncology "From: 09/06" "To: 05/12"
在PDF它會吐出如下:
Ambulatory Care 10
Home Healthcare 3
Oncology 5.8
我已經閱讀過並嘗試許多不同的代碼示例在那裏,但我只是不夠熟練使用這種類型的PHP來完成這項工作。還有其他一些事情要記住。這是在一個WordPress網站上,使用Gravity Forms作爲表單,Gravity Forms PDF Extended從這些表單創建PDF。另外要注意的是,在今年的條目必須留在yy
格式與yyyy
用戶不會在一天補爲好,只有mm/yy
任何幫助,在得到這個東西去將大大讚賞!
<?php
// Set timezone
date_default_timezone_set("UTC");
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year','month','day','hour','minute','second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Create temp time from time1 and interval
$ttime = strtotime('+1 ' . $interval, $time1);
// Set initial values
$add = 1;
$looped = 0;
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
// Create new temp time from time1 and interval
$add++;
$ttime = strtotime("+" . $add . " " . $interval, $time1);
$looped++;
}
$time1 = strtotime("+" . $looped . " " . $interval, $time1);
$diffs[$interval] = $looped;
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode(", ", $times);
}
?>
該格式是否固定?解析輸入'#^ From:(\ d {2})/(\ d {2})$#','#^ To:(\ d {2})/(\ d {2})$#'並查看DateTime,DateDiff和DateInterval類。 – m02ph3u5
格式已修復,無法更改。我現在就來看看那些,我已經有了,但是我一直無法圍繞使這些代碼工作。沒有足夠的技巧來從頭開始編寫它,並且使用上面提到的那些類來操縱其他現有代碼時遇到困難。 – Brendan
那麼你在找什麼?你可以用來計算的函數?或者你甚至看過表單句柄代碼? – David