简体   繁体   English

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

[英]PHP convert time strtotime working incorrect for me

I need to convert time, use PHP strtotime function.我需要转换时间,使用PHP strtotime 函数。

time have following format时间有以下格式

$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

How I can recognise is time 00:10 or 12:10我如何识别是时间 00:10 或 12:10

Thanks for help!感谢帮助!

If you take a look at the date reference , a lower-case h is the 12-hour formatted hour.如果您查看date reference ,小写的h是 12 小时格式的小时。 You need a capital H :你需要一个大写的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: Your time variables should be strings, with quotes.: FWIW:你的时间变量应该是字符串,带引号。:

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

You can check difference using AM or PM or use 24 hours format cause 0 and 12 same mean for time different is it's am or pm您可以使用 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 

or use capital H或使用大写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 is "12-hour format of an hour with leading zeros, 01 through 12" . h“带前导零的小时的 12 小时格式,01 到 12”
You want H instead, which is 00 through 23. See http://php.net/date .您需要H ,即 00 到 23。请参阅http://php.net/date

In the date format h is 12 hour.在日期格式中, h是 12 小时。 To differentiate you can either use G or you can add am or pm with a .为了区分您可以使用G或者你可以用加上午或下午a

Ie IE

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');

Use the 24 hour format!使用 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 ));

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

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