0

我必须执行以下操作,

我的日期有两种格式Y-m-d H:i:sY-m-d

我的要求是,

如果 date 包含 Hours( H),需要转换为F j, Y, g:i a

否则F j, Y

有什么方法可以签到HY-m-d H:i:s

提前致谢

4

4 回答 4

1

尝试这个...

if(strstr($date, ' ') !== FALSE) {
    $date = date('F j, Y, g:i a', strtotime($date));
} else {
    $date = date('F j, Y', strtotime($date));
}
于 2013-09-26T03:23:07.340 回答
1

Y-m-d日期格式是10字符长度,Y-m-d H:i:s日期格式是19字符长度。根据长度,您可以找到它是否有日期部分。即使它的长度为 19 个字符,时间部分也可能为 0,在这种情况下,您可以忽略时间部分。沿着这条线应该工作:

//  $date = "2013-09-26";
//  $date = "2013-09-26 00:00:00";
    $date = "2013-09-26 00:00:01";

if( strlen( $date ) == 10 )
{
//  Include code to convert the date to F j, Y
    echo $date , ' Does not have time part .'; 
}
else
{
    if(  strlen( $date ) == 19 and substr( $date, 10, 8 ) <> '00:00:00' )
    {
    //  Include code to convert the date to F j, Y, g:i a
        echo $date , ' Has time part which is not 0 .';
    }
    else
    {
    //  Include code to convert the date to F j, Y
        echo $date , ' Has time part but is 0 .';
    }
}
于 2013-09-26T03:30:39.927 回答
0

我从数据库中获取日期时使用的东西可能会或可能不会随时间出现。根据需要进行修改。

IF(
    TIME( {$val['field_name']} ) = '00:00:00',
    DATE_FORMAT(DATE( {$val['field_name']} ),'%D %M %Y'),
    DATE_FORMAT({$val['field_name']},'%l:%i%p, %D %M %Y')
    ) 
于 2013-09-26T03:23:00.237 回答
0

它有点冗长-正如@Bamar所说,您可以只检查空格,但是即使该字段中有前导或尾随空格,这也将起作用:

$date_format = "\s*\d\d\d\d-\d\d-\d\d\s*";
$date_time_format = "\s*\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d\s*";
if(preg_match($date_time_format, $subject)){
    $subject = date('F j, Y, g:i a', strtotime($subject));
}
elseif(preg_match($date_format, $subject)){
    $subject = date('F j, Y', strtotime($subject));
}
else{
    //formatting error - you can decide how to format.
}
于 2013-09-26T03:23:40.337 回答