简体   繁体   English

PHP时间将NULL处理为00:00:00

[英]php time handling NULL as 00:00:00

I have a database with times stored as TIME as in 00:00:00 format. 我有一个数据库,其TIME00:00:00格式存储为TIME The database accepts NULL , but I also use functions that convert the time to a 12-hour format for viewing only. 该数据库接受NULL ,但是我还使用将时间转换为12小时格式的函数,仅用于查看。

The problem is: when the time input is empty, the functions that convert the format are changing the NULL to 00:00:00 which I would be fine with, but I cannot get it to not print 12:00 AM when converting it back to 12-hour time. 问题是:当时间输入为空时,转换格式的函数会将NULL更改为00:00:00 (我可以用),但是将其转换回时我无法使其不打印12:00 AM到12小时的时间。

I need to either: 我需要:

  • Have the input go into the database as NULL not 00:00:00 将输入作为NULL而不是00:00:00进入数据库

OR 要么

  • Display nothing when converting 00:00:00 to 12-hour time. 00:00:00转换为12小时时不显示任何内容。

These are the variations of the functions I have been using, again it is working if the value is a real time. 这些是我一直在使用的功能的变体,如果该值是实时的,它也可以正常工作。

function time_12_to_24_sql ($value, $default) {
    $time = $_POST[$value];

    return ((!array_key_exists($value,$_POST)) || $_POST[$value] == NULL) ? $defaultValue : date("H:i:s", strtotime($time));
}

function time_12_to_24 ($input) {

if($input == NULL) {
    $retVal = $input;
}
if($input == 'NULL') {
    $retVal = $input;
}
if (!isset($input)) {
    $retVal = NULL;
}
if($input == '12:00 AM') {
    $retVal = NULL;
}
if($input == '') {
    $retVal = NULL;
}
else {
    $retVal = date("H:i:s", strtotime($input));
}
return $retVal;
}

function time_24_to_12 ($input) {
    if($input == NULL) {
        $retVal = $input;
    }
    if (strtotime($input) == '00:00:00') {
        $retVal = '';
    }
    if ($input == '') {
        $retVal = '';
    }
    if (!isset($input)) {
        $retVal = '';
    }
    else {
        if(strtotime($input) > 0){ 
            $retVal = date("g:i A", strtotime($input));
        }
    }
    return $retVal;
}

You're kind of abusing strtotime() here. 您在这里有点滥用strtotime() What you want to be doing is using PHP's date formatting functions: 您要使用的是PHP的日期格式功能:

function time_24_to_12 ($input) {
    // empty checks for null, empty string, zero, false, unset, etc.
    if (empty($input)) {
        return "";
    }
    $date = DateTime::createFromFormat("H:i:s", $input);
    $time = $date->format("h:i:s A");
    return ($time === "12:00:00 AM") ? "" : $time;
}

function time_12_to_24 ($input) {
    if (empty($input)) {
        return "";
    }
    $date = DateTime::createFromFormat("h:i:s A", $input);
    $time = $date->format("H:i:s");

    return ($time === "00:00:00") ? "" : $time;
}

(If you wanted to get fancy you could do a regular expression check on the input, instead of just checking for empty.) (如果想花哨的话,可以对输入进行正则表达式检查,而不仅仅是检查是否为空。)

Now this should work as you're looking for: 现在,这应该可以在您正在寻找的时候工作:

echo time_24_to_12("23:34:29") . "\n";
echo time_24_to_12("00:00:00") . "\n";
echo time_24_to_12("") . "\n";

echo time_12_to_24("11:34:29 PM") . "\n";
echo time_12_to_24("12:00:00 AM") . "\n";
echo time_12_to_24(null) . "\n";

Result: 结果:

11:34:29 PM


23:34:29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM