PHP strtotime 需要调整
PHP strtotime needs adjustment
我正在开发一个 php 函数来计算来自 wordpress 表单插件的两个用户输入时间字段之间的总时间,但是当时间超过 2400 小时时该函数不起作用。
具体情况如下:我正在尝试计算用户睡了多长时间,但当开始时间是晚上(例如 23:00)和结束时间(醒来)时,我得到一个负数是第二天早上(例如 07:00)- 这是因为时间是从显示同一天两个时间的表单中获取的,因此使用 strtotime 转换时开始时间看起来大于结束时间。
原代码如下:
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 98){ //98 is the field id from the wordpress plugin that will store the total time I'm using
$start = (strtotime($_POST['item_meta'][88])); //88 is the field id for 'go to sleep time' from the wordpress forms program - the user selects times from 00:00 to 23:00
$end = (strtotime($_POST['item_meta'][78])); //78 is the field id for 'wake up time' from the wordpress forms program - the user selects times from 00:00 to 23:00
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime; //change 25 to the ID of the hidden or admin only field which will hold the calculation
}
return $errors;
}
如果结束时间小于开始时间,我会尝试将结束时间增加 12 小时来进行调整:
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 98){
$start = (strtotime($_POST['item_meta'][88]));
$end = (strtotime($_POST['item_meta'][78]));
if ($end < $start) {
$end = ($end + 43200)
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime;
} else
{
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime;
}
}
return $errors;
如strtotime
所述
int strtotime ( string $time [, int $now = time() ] )
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
在您的情况下,这两个时间都是相对于今天进行解释的,即今天早上 7 点和今晚 11 点。
要解决这个问题,调整一整天就足够了,例如24*60*60
秒,或以明天为基准结束。所以不是两个大分支,一开始有一个小的调整,然后统一计算差异
$start = strtotime($_POST['item_meta'][88]);
$end = strtotime($_POST['item_meta'][78]);
if ($end < $start)
$end += 86400; // shift the end 24 hours into tomorrow
$totaltime = $end - $start;
无关,但您不需要手动计算小时和分钟,请使用 DateTime::format
代替
$date = DateTime::createFromFormat('U', $totaltime);
$s = $date->format('H:I');
我正在开发一个 php 函数来计算来自 wordpress 表单插件的两个用户输入时间字段之间的总时间,但是当时间超过 2400 小时时该函数不起作用。
具体情况如下:我正在尝试计算用户睡了多长时间,但当开始时间是晚上(例如 23:00)和结束时间(醒来)时,我得到一个负数是第二天早上(例如 07:00)- 这是因为时间是从显示同一天两个时间的表单中获取的,因此使用 strtotime 转换时开始时间看起来大于结束时间。
原代码如下:
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 98){ //98 is the field id from the wordpress plugin that will store the total time I'm using
$start = (strtotime($_POST['item_meta'][88])); //88 is the field id for 'go to sleep time' from the wordpress forms program - the user selects times from 00:00 to 23:00
$end = (strtotime($_POST['item_meta'][78])); //78 is the field id for 'wake up time' from the wordpress forms program - the user selects times from 00:00 to 23:00
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime; //change 25 to the ID of the hidden or admin only field which will hold the calculation
}
return $errors;
}
如果结束时间小于开始时间,我会尝试将结束时间增加 12 小时来进行调整:
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 98){
$start = (strtotime($_POST['item_meta'][88]));
$end = (strtotime($_POST['item_meta'][78]));
if ($end < $start) {
$end = ($end + 43200)
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime;
} else
{
$totaltime = ($end - $start);
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$totaltime = $hours . ':' . $minutes;
$value = $_POST['item_meta'][98] = $totaltime;
}
}
return $errors;
如strtotime
所述
int strtotime ( string $time [, int $now = time() ] )
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
在您的情况下,这两个时间都是相对于今天进行解释的,即今天早上 7 点和今晚 11 点。
要解决这个问题,调整一整天就足够了,例如24*60*60
秒,或以明天为基准结束。所以不是两个大分支,一开始有一个小的调整,然后统一计算差异
$start = strtotime($_POST['item_meta'][88]);
$end = strtotime($_POST['item_meta'][78]);
if ($end < $start)
$end += 86400; // shift the end 24 hours into tomorrow
$totaltime = $end - $start;
无关,但您不需要手动计算小时和分钟,请使用 DateTime::format
代替
$date = DateTime::createFromFormat('U', $totaltime);
$s = $date->format('H:I');