我必须做以下事情,
我的约会时间有Y-m-d H:i:s
和Y-m-d
我的要求是,
如果日期包含小时数(H
),则需要转换为F j, Y, g:i a
否则转到F j, Y
是否有任何方法可以在H
Y-m-d H:i:s
提前致谢
答案 0 :(得分:1)
试试这个......
if(strstr($date, ' ') !== FALSE) {
$date = date('F j, Y, g:i a', strtotime($date));
} else {
$date = date('F j, Y', strtotime($date));
}
答案 1 :(得分: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 .';
}
}
答案 2 :(得分: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')
)
答案 3 :(得分: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.
}