Home  >  Article  >  php教程  >  php将12小时制转换成24小时制的方法_php技巧

php将12小时制转换成24小时制的方法_php技巧

PHP中文网
PHP中文网Original
2016-05-25 17:09:061060browse

这篇文章主要介绍了php将12小时制转换成24小时制的方法,涉及php时间操作的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php将12小时制转换成24小时制的方法。分享给大家供大家参考。具体如下:

php将12小时制转换成24小时制,输入格式为:02:30:00 pm 转换成:14:30:00

<?php
function to_24_hour($hours,$minutes,$seconds,$meridiem){
 $hours = sprintf(&#39;%02d&#39;,(int) $hours);
 $minutes = sprintf(&#39;%02d&#39;,(int) $minutes);
 $seconds = sprintf(&#39;%02d&#39;,(int) $seconds);
 $meridiem = (strtolower($meridiem)==&#39;am&#39;) ? &#39;am&#39; : &#39;pm&#39;;
 return date(&#39;H:i:s&#39;, strtotime("{$hours}:{$minutes}:{$seconds}{$meridiem}"));
}
echo to_24_hour( 1, 2, 3, &#39;pm&#39; ); // Returns 13:02:03
echo to_24_hour( &#39;02&#39;, &#39;30&#39;, &#39;00&#39;, &#39;pm&#39; ); // Returns 14:30:00
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php判断文本文档字符编码Next article:AA制分账