PHP转换时间strtotime对我来说不正确


PHP convert time strtotime working incorrect for me

我需要转换时间,使用PHP strtotime函数。

时间具有以下格式

$date1 = 12:10:00
$date2 = 00:10:00
echo date( 'Y:m:d h:i:s', strtotime( $date1 )); // 12:10:00
echo date( 'Y:m:d h:i:s', strtotime( $date2 )); // 12:10:00

我如何识别时间是00:10或12:10

谢谢你的帮助!

如果查看日期引用,小写的h是12小时格式的小时。你需要一个资本H:

echo date( 'Y:m:d H:i:s', strtotime( $date1 )); // 12:10:00
echo date( 'Y:m:d H:i:s', strtotime( $date2 )); // 00:10:00

FWIW:您的时间变量应该是带引号的字符串。:

$date1 = "12:10:00";
$date2 = "00:10:00";

您可以使用AM或PM检查差异,也可以使用24小时格式,因为0和12是时间差异的平均值,是上午还是下午

$date1 = '12:10:00';
$date2 = '00:10:00';
echo date( 'Y:m:d h:i:s A', strtotime( $date1 )); // 2014:04:16 12:10:00 PM
echo date( 'Y:m:d h:i:s A', strtotime( $date2 )); // 2014:04:16 12:10:00 AM 

或使用资本H

echo date( 'Y:m:d H:i:s', strtotime( $date1 )); // 12:10:00
echo date( 'Y:m:d H:i:s', strtotime( $date2 )); // 00:10:00

h"一个小时的12小时格式,从01到12"
您需要H,即00到23。看见http://php.net/date.

在日期格式中,h为12小时。要进行区分,您可以使用G,也可以将am或pm与a一起添加。

echo date( 'Y:m:d G:i:s', strtotime( $date1 ));
echo date( 'Y:m:d G:i:s', strtotime( $date2 ));
echo date( 'Y:m:d h:i:s a', strtotime( $date1 ));
echo date( 'Y:m:d h:i:s a', strtotime( $date2 ));
$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');

使用24小时格式!

$date1 = 12:10:00
$date2 = 00:10:00
echo date( 'Y:m:d H:i:s', strtotime( $date1 ));
echo date( 'Y:m:d H:i:s', strtotime( $date2 ));